I'm new on this forum. I work in R.
I have a matrix (kxn)
and I have to considere all combinations of the rows vectors (1 xn)
taken 2, 3, 4 at a time.
Example:
Consider a matrix m = diag(c(rep(1, 3)))
,
I want the combinations of the three rows vectors (1 xn)
taken 2 at a time:
first: (1,0,0) and (0,1,0)
second: (1,0,0) and (0,0,1)
third: (0,1,0) and (0,0,1)
In a second moment, for each couple, I need to compute the sum on the columns. Any suggestion?
Thanks!
m <- diag(c(rep(1, 3)))
tmp <- combn(nrow(m), 2)
array(t(m[tmp,]), c(ncol(m), 2, ncol(tmp)))
#, , 1
#
# [,1] [,2]
#[1,] 1 0
#[2,] 0 1
#[3,] 0 0
#
#, , 2
#
# [,1] [,2]
#[1,] 1 0
#[2,] 0 0
#[3,] 0 1
#
#, , 3
#
# [,1] [,2]
#[1,] 0 0
#[2,] 1 0
#[3,] 0 1
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.