Multiply permutations of two vectors in R -
i've got 2 vectors of length 4 , want multiplication of permutations of vector:
a=(a1,a2,a3,a4) b=(b1,b2,b3,b4)
i want:
a1*b1;a1*b2;a1*b3...a4*b4
as list known order or data.frame row.names=a , colnames=b
use outer(a,b,'*')
return matrix
x<-c(1:4) y<-c(10:14) outer(x,y,'*')
returns
[,1] [,2] [,3] [,4] [,5] [1,] 10 11 12 13 14 [2,] 20 22 24 26 28 [3,] 30 33 36 39 42 [4,] 40 44 48 52 56
and if want result in list can
z<-outer(x,y,'*') z.list<-as.list(t(z))
head(z.list)
returns
[[1]] [1] 10 [[2]] [1] 11 [[3]] [1] 12 [[4]] [1] 13 [[5]] [1] 14 [[6]] [1] 20
which x1*y1, x1*y2, x1* y3, x1*y4, x2*y1 ,... (if want x1*y1, x2*y1, ... replace t(z)
z
)
Comments
Post a Comment