R - 给定一个矩阵和一个幂,生成包含矩阵列的所有唯一组合的多个矩阵

标签 r matrix cross-product

根据下面链接的相关问题(请参阅@Aleh解决方案):我希望仅计算给定幂的矩阵中列之间的唯一乘积。

例如,对于 N=5,M=3, p=2,我们得到列 (1,1)、(1,2)、(1,3)、(2,1)、(2 ,2)、(2,3)、(3,1)、(3,2)、(3,3)。我想修改(@Aleh's)代码以仅计算列(1,1)、(1,2)、(1,3)、(2,2)、(2,3)、(3,3)之间的乘积。但我想对每个第 p 个订单执行此操作。

有人可以帮我在 R 中完成这个任务吗?

非常感谢!

相关问题提问:R - Given a matrix and a power, produce multiple matrices containing all combinations of matrix columns

最佳答案

我们创建以下函数,它采用所选 p 的所有“唯一”排列,并将矩阵的相关列相乘:

fun <- function(mat,p) {
  mat <- as.data.frame(mat)
  combs <- do.call(expand.grid,rep(list(seq(ncol(mat))),p)) # all combinations including permutations of same values
  combs <- combs[!apply(combs,1,is.unsorted),]              # "unique" permutations only
  rownames(combs) <- apply(combs,1,paste,collapse="-")      # Just for display of output, we keep info of combinations in rownames
  combs <- combs[order(rownames(combs)),]                   # sort to have desired column order on output
  apply(combs,1,function(x) Reduce(`*`,mat[,x]))            # multiply the relevant columns
}

示例

N = 5
M = 3
mat1 = matrix(1:(N*M),N,M)
#      [,1] [,2] [,3]
# [1,]    1    6   11
# [2,]    2    7   12
# [3,]    3    8   13
# [4,]    4    9   14
# [5,]    5   10   15

M = 4
mat2 = matrix(1:(N*M),N,M)
#      [,1] [,2] [,3] [,4]
# [1,]    1    6   11   16
# [2,]    2    7   12   17
# [3,]    3    8   13   18
# [4,]    4    9   14   19
# [5,]    5   10   15   20

lapply(2:4,fun,mat=mat1)
# [[1]]
#      1-1 1-2 1-3 2-2 2-3 3-3
# [1,]   1   6  11  36  66 121
# [2,]   4  14  24  49  84 144
# [3,]   9  24  39  64 104 169
# [4,]  16  36  56  81 126 196
# [5,]  25  50  75 100 150 225
# 
# [[2]]
#      1-1-1 1-1-2 1-1-3 1-2-2 1-2-3 1-3-3 2-2-2 2-2-3 2-3-3 3-3-3
# [1,]     1     6    11    36    66   121   216   396   726  1331
# [2,]     8    28    48    98   168   288   343   588  1008  1728
# [3,]    27    72   117   192   312   507   512   832  1352  2197
# [4,]    64   144   224   324   504   784   729  1134  1764  2744
# [5,]   125   250   375   500   750  1125  1000  1500  2250  3375
# 
# [[3]]
#      1-1-1-1 1-1-1-2 1-1-1-3 1-1-2-2 1-1-2-3 1-1-3-3 1-2-2-2 1-2-2-3 1-2-3-3 1-3-3-3 2-2-2-2 2-2-2-3 2-2-3-3 2-3-3-3 3-3-3-3
# [1,]       1       6      11      36      66     121     216     396     726    1331    1296    2376    4356    7986   14641
# [2,]      16      56      96     196     336     576     686    1176    2016    3456    2401    4116    7056   12096   20736
# [3,]      81     216     351     576     936    1521    1536    2496    4056    6591    4096    6656   10816   17576   28561
# [4,]     256     576     896    1296    2016    3136    2916    4536    7056   10976    6561   10206   15876   24696   38416
# [5,]     625    1250    1875    2500    3750    5625    5000    7500   11250   16875   10000   15000   22500   33750   50625

fun(mat2,2)
#      1-1 1-2 1-3 1-4 2-2 2-3 2-4 3-3 3-4 4-4
# [1,]   1   6  11  16  36  66  96 121 176 256
# [2,]   4  14  24  34  49  84 119 144 204 289
# [3,]   9  24  39  54  64 104 144 169 234 324
# [4,]  16  36  56  76  81 126 171 196 266 361
# [5,]  25  50  75 100 100 150 200 225 300 400

关于R - 给定一个矩阵和一个幂,生成包含矩阵列的所有唯一组合的多个矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49538911/

相关文章:

javascript - 如何在父元素缩放时限制子元素倾斜

c - C 中的 vector 叉积

r - 在R中显示UTF-8编码的汉字

javascript - 如果我更改了数字输入的格式, Shiny 的书签不起作用

r - R 中多维尺度 (MDS) 的预测值

linux - 无法打开: HTTP status was '403 Forbidden' - The "sensor" parameter specified in the request must be set to either "true" or "false"

python - 距离矩阵的树状图或其他图

python - 将文本文件导入为 numpy 中的矩阵

python - 正则表达式替换的组合乘积

python - "cross product"但提高到指数而不是乘法