of successive rows
To: r-help@r-project.org
Received: Monday, 28 July, 2008, 8:20 AM
Hi everyone,
I want to perform an operation on a matrx that outputs the
product of
successive pairs of rows. For example: calculating the
product between rows
1 2; 3 4; 5 6...etc.
Does anyone know of any
Hi rcoder,
Assuming that the number of rows of your matrix x is even, try also:
x - matrix(1:72,12)
apply(x,2, tapply, rep(1:(nrow(x)/2),each=2),prod)
# or using a function which argument x is your matrix
prod.mat=function(x) {
k=nrow(x)
g=rep(1:(k/2),each=2)
apply(x,2, tapply, g,prod)
}
Does this do what you want:
x - matrix(1:36,6)
x
[,1] [,2] [,3] [,4] [,5] [,6]
[1,]17 13 19 25 31
[2,]28 14 20 26 32
[3,]39 15 21 27 33
[4,]4 10 16 22 28 34
[5,]5 11 17 23 29 35
[6,]6 12 18 24 30 36
this works too:
n = 6 # number of rows
m = 4 # number of coloumns
nm = n*m
mat = matrix(1:nm,n) # your matrix
pf = function(Col){
ind = rep(1:(n/2),each=2)
out = tapply(Col,ind,prod)
out
}
# pf performs forall vecotr x: x[i]*x[i-1],
Hi everyone,
I want to perform an operation on a matrx that outputs the product of
successive pairs of rows. For example: calculating the product between rows
1 2; 3 4; 5 6...etc.
Does anyone know of any readily available functions that can do this?
Thanks,
rcoder
--
View this message
5 matches
Mail list logo