[R] efficiently multiply different matrices in 3-d array with different vectors?

arun smartpink111 at yahoo.com
Fri Dec 28 07:03:19 CET 2012


HI,

Not sure if this is what you wanted:
set.seed(14)
Z<-array(sample(1:100,80,replace=TRUE),dim=c(5,2,8))
set.seed(21)
Y<-matrix(sample(1:40,40,replace=TRUE),nrow=8)
do.call(cbind,lapply(seq_len(dim(Z)[1]),function(i) Z[i,,]%*%Y[,i]))
#     [,1] [,2]  [,3]  [,4]  [,5]
#[1,] 5065 6070 12328 11536 13678
#[2,] 6152 9878 11161 13777  7991
A.K.




----- Original Message -----
From: Ranjan Maitra <maitra.mbox.ignored at inbox.com>
To: r-help at r-project.org
Cc: 
Sent: Friday, December 28, 2012 12:33 AM
Subject: [R] efficiently multiply different matrices in 3-d array with different vectors?

Hello,

I have been wondering of an efficient way to do this:

I have an n x m x p array Z and a p x n matrix Y.

I want to multiply each of the n matrices with the corresponding column
vector of Y.

In other words, I am wanting to matrix multiply: 

Z[i, ,] %*% Y[, i]

which will give me a (two-dimensional) array or matrix of dimension n x
p with the i'th row storing the above.

Any pointers on an efficient way of doing this? I considered using
apply, but was not successful. 

Many thanks again and best wishes,
Ranjan



-- 
Important Notice: This mailbox is ignored: e-mails are set to be
deleted on receipt. For those needing to send personal or professional
e-mail, please use appropriate addresses.

____________________________________________________________
FREE ONLINE PHOTOSHARING - Share your photos online with your friends and family!
Visit http://www.inbox.com/photosharing to find out more!

______________________________________________
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.





More information about the R-help mailing list