R语言 将块外对角元素替换为0

uyhoqukh  于 2023-03-15  发布在  其他
关注(0)|答案(4)|浏览(186)

我在R中工作,我有一个6n × 6n的矩阵。我想用零替换6x6块对角矩阵中的所有元素。有什么建议吗?
例如(仅报告12x12样本)

# create a sample 12x12 matrix
> mat <- matrix(1:144, nrow=12, ncol=12)
> mat
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] 
 [1,]    1   13   25   37   49   61   73   85   97   109   121   133
 [2,]    2   14   26   38   50   62   74   86   98   110   122   134
 [3,]    3   15   27   39   51   63   75   87   99   111   123   135
 [4,]    4   16   28   40   52   64   76   88  100   112   124   136
 [5,]    5   17   29   41   53   65   77   89  101   113   125   137
 [6,]    6   18   30   42   54   66   78   90  102   114   126   138
 [7,]    7   19   31   43   55   67   79   91  103   115   127   139
 [8,]    8   20   32   44   56   68   80   92  104   116   128   140
 [9,]    9   21   33   45   57   69   81   93  105   117   129   141
[10,]   10   22   34   46   58   70   82   94  106   118   130   142
[11,]   11   23   35   47   59   71   83   95  107   119   131   143
[12,]   12   24   36   48   60   72   84   96  108   120   132   144

我想得到的是

new_mat
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] 
 [1,]    1   13   25   37   49   61    0    0    0     0     0     0  
 [2,]    2   14   26   38   50   62    0    0    0     0     0     0  
 [3,]    3   15   27   39   51   63    0    0    0     0     0     0 
 [4,]    4   16   28   40   52   64    0    0    0     0     0     0  
 [5,]    5   17   29   41   53   65    0    0    0     0     0     0  
 [6,]    6   18   30   42   54   66    0    0    0     0     0     0  
 [7,]    0    0    0    0    0    0   79   91  103   115   127   139
 [8,]    0    0    0    0    0    0   80   92  104   116   128   140
 [9,]    0    0    0    0    0    0   81   93  105   117   129   141
[10,]    0    0    0    0    0    0   82   94  106   118   130   142
[11,]    0    0    0    0    0    0   83   95  107   119   131   143
[12,]    0    0    0    0    0    0   84   96  108   120   132   144
k4emjkb1

k4emjkb11#

取一个单位矩阵和一个全1矩阵的克罗内克积,然后乘以mat。

mat * (diag(nrow(mat) / 6) %x% matrix(1, 6, 6))

给出:

[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
 [1,]    1   13   25   37   49   61    0    0    0     0     0     0
 [2,]    2   14   26   38   50   62    0    0    0     0     0     0
 [3,]    3   15   27   39   51   63    0    0    0     0     0     0
 [4,]    4   16   28   40   52   64    0    0    0     0     0     0
 [5,]    5   17   29   41   53   65    0    0    0     0     0     0
 [6,]    6   18   30   42   54   66    0    0    0     0     0     0
 [7,]    0    0    0    0    0    0   79   91  103   115   127   139
 [8,]    0    0    0    0    0    0   80   92  104   116   128   140
 [9,]    0    0    0    0    0    0   81   93  105   117   129   141
[10,]    0    0    0    0    0    0   82   94  106   118   130   142
[11,]    0    0    0    0    0    0   83   95  107   119   131   143
[12,]    0    0    0    0    0    0   84   96  108   120   132   144
xuo3flqw

xuo3flqw2#

试试看

library(Matrix)
as.matrix(bdiag(replicate(2, matrix(1, 6, 6), simplify = FALSE)) * mat)
  • 输出
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
 [1,]    1   13   25   37   49   61    0    0    0     0     0     0
 [2,]    2   14   26   38   50   62    0    0    0     0     0     0
 [3,]    3   15   27   39   51   63    0    0    0     0     0     0
 [4,]    4   16   28   40   52   64    0    0    0     0     0     0
 [5,]    5   17   29   41   53   65    0    0    0     0     0     0
 [6,]    6   18   30   42   54   66    0    0    0     0     0     0
 [7,]    0    0    0    0    0    0   79   91  103   115   127   139
 [8,]    0    0    0    0    0    0   80   92  104   116   128   140
 [9,]    0    0    0    0    0    0   81   93  105   117   129   141
[10,]    0    0    0    0    0    0   82   94  106   118   130   142
[11,]    0    0    0    0    0    0   83   95  107   119   131   143
[12,]    0    0    0    0    0    0   84   96  108   120   132   144
h7appiyu

h7appiyu3#

您可以像下面这样使用kronecker创建一个掩码矩阵

> mat * kronecker(diag(c(1, 1)), matrix(1, 6, 6))
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
 [1,]    1   13   25   37   49   61    0    0    0     0     0     0
 [2,]    2   14   26   38   50   62    0    0    0     0     0     0
 [3,]    3   15   27   39   51   63    0    0    0     0     0     0
 [4,]    4   16   28   40   52   64    0    0    0     0     0     0
 [5,]    5   17   29   41   53   65    0    0    0     0     0     0
 [6,]    6   18   30   42   54   66    0    0    0     0     0     0
 [7,]    0    0    0    0    0    0   79   91  103   115   127   139
 [8,]    0    0    0    0    0    0   80   92  104   116   128   140
 [9,]    0    0    0    0    0    0   81   93  105   117   129   141
[10,]    0    0    0    0    0    0   82   94  106   118   130   142
[11,]    0    0    0    0    0    0   83   95  107   119   131   143
[12,]    0    0    0    0    0    0   84   96  108   120   132   144
vxf3dgd4

vxf3dgd44#

您也可以直接进行替换。这种方法的一个好处是它不依赖于基准表的类型或替换值。

## Number of rows in square matrix
n <- nrow(mat)
## Number of rows in rectangular block
b <- 6L

## Index of block diagonal elements
k <- sequence(rep.int(b, n), 
              rep(seq.int(1L, n, b), each = b) + seq.int(0L, n * n - 1L, n))

## Replacement
mat[-k] <- 0L
mat
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
 [1,]    1   13   25   37   49   61    0    0    0     0     0     0
 [2,]    2   14   26   38   50   62    0    0    0     0     0     0
 [3,]    3   15   27   39   51   63    0    0    0     0     0     0
 [4,]    4   16   28   40   52   64    0    0    0     0     0     0
 [5,]    5   17   29   41   53   65    0    0    0     0     0     0
 [6,]    6   18   30   42   54   66    0    0    0     0     0     0
 [7,]    0    0    0    0    0    0   79   91  103   115   127   139
 [8,]    0    0    0    0    0    0   80   92  104   116   128   140
 [9,]    0    0    0    0    0    0   81   93  105   117   129   141
[10,]    0    0    0    0    0    0   82   94  106   118   130   142
[11,]    0    0    0    0    0    0   83   95  107   119   131   143
[12,]    0    0    0    0    0    0   84   96  108   120   132   144

话虽如此,对于"double""complex"类型的矩阵,将元素乘上合适的克罗内克积可能是正确的(最透明的)答案。

相关问题