如何找到R中矩阵的所有可能排列? [英] How to find all the possible permutations of a matrix in R?

查看:101
本文介绍了如何找到R中矩阵的所有可能排列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个矩阵,例如5x5.

I have a matrix, for example, 5x5.

     [,1] [,2] [,3] [,4] [,5]
[1,]   22   -2   -2   -2    2
[2,]   -2   22    2    2    2
[3,]   -2    2   22    2    2
[4,]   -2    2    2   22    2
[5,]    2    2    2    2   22.

如您所见,矩阵是对称的.在主对角线上有4 + 3 + 2 + 1 = 10个位置,我通过 combn 找到了所有可能的(置换)矩阵,在这10个位置中有(-2)3次.这意味着10!/3!* 7!= 120矩阵.

As you can see, the matrix is symmetric. Above the main diagonal, there are 4+3+2+1=10 positions, and I find via combn all the possible (permutation) matrices, which have (-2) 3 times in these 10 positions. That means 10!/3!*7!=120 matrices.

但是其中一些是等效的.

But some of them are equivalent.

所以,我的问题是如何从120中查找非等价矩阵.

So,my problem is how to find the non-equivalent matrices from the 120.

我正在考虑置换矩阵,因为如果我选择120个矩阵之一,并且使用 rmperm ,结果我将得到120个矩阵之一(随机).

I am taking about permutation matrices, because if I pick one of the 120 matrices and I use rmperm, I have as a result one (random) of the 120 matrices.

当我有5x5和6x6矩阵时,我没有问题,因为我已经开发了一种算法.但是现在我想在7x7矩阵或更多矩阵中执行相同的操作,但是该算法非常慢,因为我有很多循环.

When I have 5x5 and 6x6 matrices, I don't have problem, because I have developed an algorithm. But now I want to do the same in a 7x7 matrix and more, but the algorithm is very slow, because I have lots of loops.

因此,当我从120个矩阵中选择一个矩阵时,我想用一个命令将120个矩阵中的所有排列矩阵给我.

So, I want with one command, when I pick a matrix from the 120 matrices, to give me ALL the permutations matrices from the 120.

非常感谢!

推荐答案

基本上,您想要的是多集置换.程序包iterpc将完成这项工作.

Basically what you want is permutation of multiset. The package iterpc will do the job.

> library(iterpc)
> I <- iterpc(c(3,7), ordered=TRUE)
> getlength(I)
[1] 120
> getall(I)
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
  [1,]    1    1    1    2    2    2    2    2    2     2
  [2,]    1    1    2    1    2    2    2    2    2     2
  [3,]    1    1    2    2    1    2    2    2    2     2
  [4,]    1    1    2    2    2    1    2    2    2     2
  [5,]    1    1    2    2    2    2    1    2    2     2
  [6,]    1    1    2    2    2    2    2    1    2     2
  [7,]    1    1    2    2    2    2    2    2    1     2
  [8,]    1    1    2    2    2    2    2    2    2     1
  [9,]    1    2    1    1    2    2    2    2    2     2
 [10,]    1    2    1    2    1    2    2    2    2     2
 [11,]    1    2    1    2    2    1    2    2    2     2
 [12,]    1    2    1    2    2    2    1    2    2     2
 [13,]    1    2    1    2    2    2    2    1    2     2
 [14,]    1    2    1    2    2    2    2    2    1     2
 [15,]    1    2    1    2    2    2    2    2    2     1
 [16,]    1    2    2    1    1    2    2    2    2     2
 [17,]    1    2    2    1    2    1    2    2    2     2
 [18,]    1    2    2    1    2    2    1    2    2     2
 [19,]    1    2    2    1    2    2    2    1    2     2
 [20,]    1    2    2    1    2    2    2    2    1     2
 [ reached getOption("max.print") -- omitted 100 rows ]

这里的每一行都是1和2的排列.您应该将-1替换为-2.

Each row here is a permutations of 1 and 2. You should replace the 1's by -2.

这篇关于如何找到R中矩阵的所有可能排列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆