Given a matrix mat
(of size N
by M
) and a power, p
(e.g., 4), produce p
matrices, where each p
-th matrix contains all possible combinations of the columns in mat
at that degree.
In my current approach, I generate the p
-th matrix and then use it in the next call to produce the p+1
th matrix. Can this be 'automated' for a given power p
, rather than done manually?
I am a novice when it comes to R and understand that there is likely a more efficient and elegant way to achieve this solution than the following attempt...
N = 5
M = 3
p = 4
mat = matrix(1:(N*M),N,M)
mat_1 = mat
mat_2 = t(sapply(1:N, function(i) tcrossprod(mat_1[i, ], mat[i, ])))
mat_3 = t(sapply(1:N, function(i) tcrossprod(mat_2[i, ], mat[i, ])))
mat_4 = t(sapply(1:N, function(i) tcrossprod(mat_3[i, ], mat[i, ])))
Can anyone provide some suggestions? My goal is to create a function for a given matrix mat
and power p
that outputs the p
different matrices in a more 'automated' fashion.
Related question that got me started: How to multiply columns of two matrix with all combinations