[R] elementwise matrix multiplication with a special structure

David L Carlson dcarlson at tamu.edu
Fri Oct 23 21:25:26 CEST 2015


Don't use html formatted emails. The r-help server strips the html formatting leaving us the the mess you see below. 

You don't need any loops:

> aa <- matrix(1:9, 3, 3)
> bb <- aa * 10
> cc <- bb * 10
> x <- as.matrix(expand.grid(k=1:3, j=1:3, c=1:3, r=1:3))
> str(x)
 int [1:81, 1:4] 1 2 3 1 2 3 1 2 3 1 ...
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr [1:4] "k" "j" "c" "r"
> dd <- aa[x[, c("j", "k")]] * bb[x[, c("r", "j")]] * cc[x[, c("k", "c")]]
> dd
 [1]   1000   8000  21000   8000  40000  96000  21000  84000 189000
[10]   4000  20000  42000  32000 100000 192000  84000 210000 378000
[19]   7000  32000  63000  56000 160000 288000 147000 336000 567000
[28]   2000  16000  42000  10000  50000 120000  24000  96000 216000
[37]   8000  40000  84000  40000 125000 240000  96000 240000 432000
[46]  14000  64000 126000  70000 200000 360000 168000 384000 648000
[55]   3000  24000  63000  12000  60000 144000  27000 108000 243000
[64]  12000  60000 126000  48000 150000 288000 108000 270000 486000
[73]  21000  96000 189000  84000 240000 432000 189000 432000 729000

# Or a bit more compactly
> dd <- aa[x[, c(2, 1)]] * bb[x[, c(4, 2)]] * cc[x[, c(1, 3)]]

-------------------------------------
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77840-4352



-----Original Message-----
From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of eugen pircalabelu via R-help
Sent: Friday, October 23, 2015 12:31 PM
To: r-help at r-project.org
Subject: [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]]

______________________________________________
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.


More information about the R-help mailing list