[R] avoiding loops

From: Ingmar Visser <i.visser_at_uva.nl>
Date: Wed, 26 Mar 2008 22:58:16 +0100


Hi,
I need to compute an array from a matrix and an array:

A <- array(1:20,c(2,2,5))
B <- matrix(1:10,5)

And I would like the result to be an array consisting of the following:

rbind(A[1,,1]*B[1,],
A[2,,1]*B[1,])

rbind(A[1,,2]*B[2,],
A[2,,2]*B[2,])

rbind(A[1,,3]*B[2,],
A[2,,3]*B[2,])

etc.

Hence the result should have the same dimension as A, ie a series of 2 by 2 matrices.

Short of a for loop over the the last index of A I have struggled with versions of apply but
to no avail ...

Any insights much appreciated,

Best, Ingmar



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. Received on Wed 26 Mar 2008 - 22:02:22 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 27 Mar 2008 - 02:30:26 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive