eugen pircalabelu
2015-Oct-23 17:31 UTC
[R] elementwise matrix multiplication with a special structure
Hello R users, I have the following annoying matrix multiplication and I do not know how to avoid the nested loops, so maybe someone can help me with some ideas. I have searched the forum for past posts but nothing came up.Here it is: ?aa=matrix(1:9,3,3)?bb=matrix(seq(10,90,by=10),3,3)?cc=matrix(seq(100,900, by=100),3,3)?dd=NULL for(r in 1:3){????for(c in 1:3){????????for(j in 1:3){????????????for(k in 1:3){dd=c(dd,aa[j,k]*bb[r,j]*cc[k,c])}}}}dd?[1] ? 1000 ? 8000 ?21000 ? 8000 ?40000 ?96000 ?21000 ?84000 189000 ? 4000[11] ?20000 ?42000 ?32000 100000 192000 ?84000 210000 378000 ? 7000 ?32000[21] ?63000 ?56000 160000 288000 147000 336000 567000 ? 2000 ?16000 ?42000[31] ?10000 ?50000 120000 ?24000 ?96000 216000 ? 8000 ?40000 ?84000 ?40000[41] 125000 240000 ?96000 240000 432000 ?14000 ?64000 126000 ?70000 200000[51] 360000 168000 384000 648000 ? 3000 ?24000 ?63000 ?12000 ?60000 144000[61] ?27000 108000 243000 ?12000 ?60000 126000 ?48000 150000 288000 108000[71] 270000 486000 ?21000 ?96000 189000 ?84000 240000 432000 189000 432000[81] 729000 What I want to obtain is the content of the vector dd in an efficient (fast) and clever way.Thank you very much and have a great day ahead!Eugen [[alternative HTML version deleted]]