Hi
I want to sum over one of the dimensions of a n x k1 x k2 array in
which each column is the product of the corresponding columns from two
matrices with dimensions n x k1 and n x k2. I can see two approaches:
a loop on k1 and a loop on k2. But I cannot figure a solution that
avoids the loop? Is it possible? (I don't refer to apply or lapply etc
either as they are just hidden loops so, correct me if I'm wrong, I
can't see they hold any real advantage here). The following does
exactly what I want (except for the loops). My aim is to find the
solution which minimises processing time.
cheers
Dave
## Basic paramters
k1=3
k2=2
n=10
m1 = matrix (1:(n*k1), nrow=n, ncol=k1)
m2 = matrix (1:(n*k2), nrow=n, ncol=k2)
## Approach 1: loop on k1
output1 = matrix(0,nrow=n,ncol=k2)
pt1 = proc.time(for (i in 1:k1) output1 = output1 + m1[,i]*m2)
## Approach 2: loop on k2
output2 = matrix(0,nrow=n,ncol=k2)
pt2 = proc.time(for (i in 1:k2) output2[,i] = rowSums( m1*m2[,i] ))
## Same result
sum(output1-output2)
--
David Pleydell
Laboratoire de Biologie Environnementale
USC INRA-EA 3184
Universit? de Franche-Comt?
Place Leclerc F
25030
Besan?on
France
(0033) 0381665763
dpleydel at univ-fcomte.fr