A quick way to get all pairs of matrix elements of columns by elements

Let's say I have a numerical value matrix:

set.seed(1)
mat <- matrix(rnorm(1000), ncol = 100)

I want to generate all vectors that are the result of an elementary product of all unique pairs of vectors in mat.

How can we improve the code below:

all.pairs <- t(combn(1:ncol(mat), 2))

res <-
  do.call(cbind,
          lapply(1:nrow(all.pairs),
                 function(p) mat[, all.pairs[p, 1]] * mat[, all.pairs[p, 2]]))
+4
source share
1 answer

We could do:

n <- ncol(mat)
lst <- lapply(1:n, function (i) mat[,i] * mat[,i:n])
do.call(cbind, lst)

Or, an even faster way:

n <- ncol(mat)
j1 <- rep.int(1:n, n:1)
j2 <- sequence(n:1) - 1L + j1
mat[, j1] * mat[, j2]

Note. The above will include multiplying the column by itself. If you want to ban it, use

n <- ncol(mat)
lst <- lapply(1:(n-1), function (i) mat[,i] * mat[,(i+1):n])
do.call(cbind, lst)

and

n <- ncol(mat)
j1 <- rep.int(1:(n-1), (n-1):1)
j2 <- sequence((n-1):1) + j1
mat[, j1] * mat[, j2]

In fact, j1and j2by the above are only the 1st and 2nd rows combn(1:ncol(mat),2). So, if you still want to stay with combn, use

all.pairs <- combn(1:ncol(mat),2)
mat[, all.pairs[1,]] * mat[, all.pairs[2,]]
+8

Source: https://habr.com/ru/post/1658248/


All Articles