[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


Not sure if this is what you wanted:
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

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


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,

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