我有一个现有的块对角协方差矩阵,我想以这样一种方式对它进行排序,即块结构很容易看到(一些函数,如blockdiag,可以自动完成此操作,但我继承了一个过程,它不会这样做)。也就是说,现在我的行和列不是按照各个块的顺序排列的。我有2个组:第1组:“a”、“B”第2组:“c”、“d”我想要的是
a b c d a 1 3 0 0 b 9 11 0 0 c 0 0 6 8 d 0 0 14 16
ni65a41a1#
看看seriation包。从块对角矩阵开始:
seriation
m <- diag(1:5) %x% matrix(1, 2, 2) m #> [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] #> [1,] 1 1 0 0 0 0 0 0 0 0 #> [2,] 1 1 0 0 0 0 0 0 0 0 #> [3,] 0 0 2 2 0 0 0 0 0 0 #> [4,] 0 0 2 2 0 0 0 0 0 0 #> [5,] 0 0 0 0 3 3 0 0 0 0 #> [6,] 0 0 0 0 3 3 0 0 0 0 #> [7,] 0 0 0 0 0 0 4 4 0 0 #> [8,] 0 0 0 0 0 0 4 4 0 0 #> [9,] 0 0 0 0 0 0 0 0 5 5 #> [10,] 0 0 0 0 0 0 0 0 5 5
Shuffle :
mm <- m[sample(1:10), sample(1:10)] mm #> [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] #> [1,] 0 0 0 0 2 0 0 0 0 2 #> [2,] 0 0 0 0 0 5 5 0 0 0 #> [3,] 0 3 3 0 0 0 0 0 0 0 #> [4,] 0 0 0 0 2 0 0 0 0 2 #> [5,] 0 0 0 4 0 0 0 0 4 0 #> [6,] 0 0 0 4 0 0 0 0 4 0 #> [7,] 0 0 0 0 0 5 5 0 0 0 #> [8,] 1 0 0 0 0 0 0 1 0 0 #> [9,] 0 3 3 0 0 0 0 0 0 0 #> [10,] 1 0 0 0 0 0 0 1 0 0
连续的混洗矩阵:
ser <- seriation::seriate(mm) mm[ser[[1]], ser[[2]]] #> [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] #> [1,] 5 5 0 0 0 0 0 0 0 0 #> [2,] 5 5 0 0 0 0 0 0 0 0 #> [3,] 0 0 1 1 0 0 0 0 0 0 #> [4,] 0 0 1 1 0 0 0 0 0 0 #> [5,] 0 0 0 0 2 2 0 0 0 0 #> [6,] 0 0 0 0 2 2 0 0 0 0 #> [7,] 0 0 0 0 0 0 3 3 0 0 #> [8,] 0 0 0 0 0 0 3 3 0 0 #> [9,] 0 0 0 0 0 0 0 0 4 4 #> [10,] 0 0 0 0 0 0 0 0 4 4
1条答案
按热度按时间ni65a41a1#
看看
seriation
包。从块对角矩阵开始:
Shuffle :
连续的混洗矩阵: