Suppose I have following list : mat <- vector("list") for (i in 1:4) mat[[i]] <- matrix(rnorm(25),5) mat> mat[[1]] [,1] [,2] [,3] [,4] [,5] [1,] -1.27171814 -0.8087277 -0.4771356 0.6001265 0.9842248 [2,] -1.37337445 1.0754536 -1.6304287 -0.6854075 -0.6029390 [3,] -0.32393457 -0.8933633 -0.9077967 0.9738039 -3.0220266 [4,] -1.22184968 -1.2571625 -0.4064736 -1.6686981 -0.4896512 [5,] -0.09333244 1.4880530 1.6410411 -0.5489993 1.0939736 [[2]] [,1] [,2] [,3] [,4] [,5] [1,] -1.0139178 -0.4817983 -0.2141111 0.1649356 1.7750268 [2,] 1.5544890 0.6345386 1.6307417 -2.0887206 1.8299279 [3,] -0.3575260 -0.6669547 0.5960779 -1.1091696 -0.2831409 [4,] 0.7742701 0.8313172 0.4178063 -2.5993914 -0.9920140 [5,] -0.6238547 0.8777678 -1.4249051 -0.4793574 0.5290309 [[3]] [,1] [,2] [,3] [,4] [,5] [1,] -1.2833578 0.2271013 -1.605616640 0.3379672 0.86307678 [2,] 0.8928252 -0.2826024 -0.294600779 0.3659292 -0.06884467 [3,] -0.8237363 1.2258510 1.091578624 1.6907392 -0.42073560 [4,] -0.2537396 0.3662041 0.009558159 1.2127068 -1.14449393 [5,] -0.5001592 0.5390412 0.372628875 -0.9434233 -0.52022201 [[4]] [,1] [,2] [,3] [,4] [,5] [1,] 0.55638191 2.000902039 -0.40155111 0.6214637 0.4044958 [2,] 0.06705861 -0.005629049 0.05144204 0.4910031 -0.2711823 [3,] -2.07173018 -0.210809259 0.22680564 0.5624870 -2.1322234 [4,] 0.04591303 -0.735466145 -0.13164334 -1.8873585 -1.2711131 [5,] 0.67454754 0.911601123 1.34304395 2.2798986 -0.5136242 Define another vector : vect <- rnorm(4) Now I want to do following calculation for (i in 1:4) mat[[i]] <- mat[[i]] * vect[i] mat My question is, can I do previous calculation without having "for" loop? Thanks -- View this message in context: http://www.nabble.com/A-question-on-List-tp25075922p25075922.html Sent from the R help mailing list archive at Nabble.com.
Hi mapply("*", vect, mat, SIMPLIFY=F) Regards Petr r-help-bounces at r-project.org napsal dne 21.08.2009 10:53:19:> > Suppose I have following list : > > mat <- vector("list") > for (i in 1:4) mat[[i]] <- matrix(rnorm(25),5) > mat > > > mat > [[1]] > [,1] [,2] [,3] [,4] [,5] > [1,] -1.27171814 -0.8087277 -0.4771356 0.6001265 0.9842248 > [2,] -1.37337445 1.0754536 -1.6304287 -0.6854075 -0.6029390 > [3,] -0.32393457 -0.8933633 -0.9077967 0.9738039 -3.0220266 > [4,] -1.22184968 -1.2571625 -0.4064736 -1.6686981 -0.4896512 > [5,] -0.09333244 1.4880530 1.6410411 -0.5489993 1.0939736 > > [[2]] > [,1] [,2] [,3] [,4] [,5] > [1,] -1.0139178 -0.4817983 -0.2141111 0.1649356 1.7750268 > [2,] 1.5544890 0.6345386 1.6307417 -2.0887206 1.8299279 > [3,] -0.3575260 -0.6669547 0.5960779 -1.1091696 -0.2831409 > [4,] 0.7742701 0.8313172 0.4178063 -2.5993914 -0.9920140 > [5,] -0.6238547 0.8777678 -1.4249051 -0.4793574 0.5290309 > > [[3]] > [,1] [,2] [,3] [,4] [,5] > [1,] -1.2833578 0.2271013 -1.605616640 0.3379672 0.86307678 > [2,] 0.8928252 -0.2826024 -0.294600779 0.3659292 -0.06884467 > [3,] -0.8237363 1.2258510 1.091578624 1.6907392 -0.42073560 > [4,] -0.2537396 0.3662041 0.009558159 1.2127068 -1.14449393 > [5,] -0.5001592 0.5390412 0.372628875 -0.9434233 -0.52022201 > > [[4]] > [,1] [,2] [,3] [,4] [,5] > [1,] 0.55638191 2.000902039 -0.40155111 0.6214637 0.4044958 > [2,] 0.06705861 -0.005629049 0.05144204 0.4910031 -0.2711823 > [3,] -2.07173018 -0.210809259 0.22680564 0.5624870 -2.1322234 > [4,] 0.04591303 -0.735466145 -0.13164334 -1.8873585 -1.2711131 > [5,] 0.67454754 0.911601123 1.34304395 2.2798986 -0.5136242 > > Define another vector : > > vect <- rnorm(4) > > > Now I want to do following calculation > > for (i in 1:4) mat[[i]] <- mat[[i]] * vect[i] > mat > > > My question is, can I do previous calculation without having "for" loop? > > Thanks > > -- > View this message in context: http://www.nabble.com/A-question-on-List- > tp25075922p25075922.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
Try this, mat <- replicate(4, matrix(rnorm(25), 5), simpl=F) mat vect <- rnorm(4) mapply( `*` , mat, vect, SIMPLIFY=F) HTH, baptiste PS: I see someone replied already, but you might find replicate useful too 2009/8/21 RON70 <ron_michael70 at yahoo.com>:> > Suppose I have following list : > > mat <- vector("list") > for (i in 1:4) mat[[i]] <- matrix(rnorm(25),5) > mat > >> mat > [[1]] > ? ? ? ? ? ?[,1] ? ? ? [,2] ? ? ? [,3] ? ? ? [,4] ? ? ? [,5] > [1,] -1.27171814 -0.8087277 -0.4771356 ?0.6001265 ?0.9842248 > [2,] -1.37337445 ?1.0754536 -1.6304287 -0.6854075 -0.6029390 > [3,] -0.32393457 -0.8933633 -0.9077967 ?0.9738039 -3.0220266 > [4,] -1.22184968 -1.2571625 -0.4064736 -1.6686981 -0.4896512 > [5,] -0.09333244 ?1.4880530 ?1.6410411 -0.5489993 ?1.0939736 > > [[2]] > ? ? ? ? ? [,1] ? ? ? [,2] ? ? ? [,3] ? ? ? [,4] ? ? ? [,5] > [1,] -1.0139178 -0.4817983 -0.2141111 ?0.1649356 ?1.7750268 > [2,] ?1.5544890 ?0.6345386 ?1.6307417 -2.0887206 ?1.8299279 > [3,] -0.3575260 -0.6669547 ?0.5960779 -1.1091696 -0.2831409 > [4,] ?0.7742701 ?0.8313172 ?0.4178063 -2.5993914 -0.9920140 > [5,] -0.6238547 ?0.8777678 -1.4249051 -0.4793574 ?0.5290309 > > [[3]] > ? ? ? ? ? [,1] ? ? ? [,2] ? ? ? ? [,3] ? ? ? [,4] ? ? ? ?[,5] > [1,] -1.2833578 ?0.2271013 -1.605616640 ?0.3379672 ?0.86307678 > [2,] ?0.8928252 -0.2826024 -0.294600779 ?0.3659292 -0.06884467 > [3,] -0.8237363 ?1.2258510 ?1.091578624 ?1.6907392 -0.42073560 > [4,] -0.2537396 ?0.3662041 ?0.009558159 ?1.2127068 -1.14449393 > [5,] -0.5001592 ?0.5390412 ?0.372628875 -0.9434233 -0.52022201 > > [[4]] > ? ? ? ? ? ?[,1] ? ? ? ? [,2] ? ? ? ?[,3] ? ? ? [,4] ? ? ? [,5] > [1,] ?0.55638191 ?2.000902039 -0.40155111 ?0.6214637 ?0.4044958 > [2,] ?0.06705861 -0.005629049 ?0.05144204 ?0.4910031 -0.2711823 > [3,] -2.07173018 -0.210809259 ?0.22680564 ?0.5624870 -2.1322234 > [4,] ?0.04591303 -0.735466145 -0.13164334 -1.8873585 -1.2711131 > [5,] ?0.67454754 ?0.911601123 ?1.34304395 ?2.2798986 -0.5136242 > > Define another vector : > > vect <- rnorm(4) > > > Now I want to do following calculation > > for (i in 1:4) mat[[i]] <- mat[[i]] * vect[i] > mat > > > My question is, can I do previous calculation without having "for" loop? > > Thanks > > -- > View this message in context: http://www.nabble.com/A-question-on-List-tp25075922p25075922.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- _____________________________ Baptiste Augui? School of Physics University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK http://newton.ex.ac.uk/research/emag