从R中的现有矩阵创建三对角矩阵

时间:2018-04-18 17:22:34

标签: r matrix

我有以下9x9矩阵。

m<-matrix(runif(9*9), nrow = 9, ncol=9)
m

     [,1]       [,2]       [,3]       [,4]       [,5]       [,6]       [,7]       [,8]       [,9]
[1,] 0.2449707 0.81326836 0.72297582 0.03551436 0.82410051 0.29909943 0.13396759 0.71995057 0.03458879
[2,] 0.2715794 0.08984877 0.36775262 0.87896437 0.51941450 0.31631194 0.19142086 0.80257639 0.05081064
[3,] 0.3741446 0.05950594 0.87952538 0.45086034 0.20399313 0.42205156 0.10122543 0.04948101 0.58620932
[4,] 0.8472067 0.56675295 0.52710442 0.35257950 0.70820007 0.30591402 0.25243479 0.81976209 0.06595630
[5,] 0.1034456 0.18511441 0.70661040 0.59577040 0.87518085 0.16782022 0.98967611 0.96391309 0.86283653
[6,] 0.3685605 0.08227258 0.24782776 0.12557143 0.97220534 0.70517827 0.01113961 0.19896752 0.45398202
[7,] 0.1305617 0.29664856 0.21321683 0.83940964 0.91064918 0.67109195 0.92055064 0.08246438 0.86575867
[8,] 0.8799545 0.52515529 0.09592935 0.43177214 0.62089808 0.04079269 0.62836857 0.70525627 0.04421984
[9,] 0.4941711 0.28413734 0.02130229 0.77447519 0.02323584 0.52432477 0.02130935 0.42077756 0.02348518

如何创建一个3块的三角形矩阵,如下图所示?我如何创建一个看起来像下面镜像的矩阵(即三对角线中的零点,三对角线中的非零点)?

     [,1]       [,2]       [,3]       [,4]       [,5]       [,6]       [,7]       [,8]       [,9]
[1,] 0.2449707 0.81326836 0.72297582  0          0          0          0          0          0 
[2,] 0.2715794 0.08984877 0.36775262  0          0          0          0          0          0 
[3,] 0.3741446 0.05950594 0.87952538  0          0          0          0          0          0 
[4,] 0         0         0            0.35257950 0.70820007 0.30591402 0          0         0            
[5,] 0         0         0            0.59577040 0.87518085 0.16782022 0          0         0            
[6,] 0         0         0            0.12557143 0.97220534 0.70517827 0          0         0            
[7,] 0         0         0            0          0          0          0.92055064 0.08246438 0.86575867
[8,] 0         0         0            0          0          0          0.62836857 0.70525627 0.04421984
[9,] 0         0         0            0          0          0          0.02130935 0.42077756 0.02348518

提前致谢!

2 个答案:

答案 0 :(得分:2)

One option is to use kronecker and multiply the repeatable matrix(diagonally) as many times as you want.

set.seed(1)
m<-matrix(runif(9*9), nrow = 9, ncol=9)

m[kronecker(diag(3), matrix(rep(TRUE,9),3))!=1] <- 0
m
#           [,1]       [,2]      [,3]      [,4]      [,5]      [,6]      [,7]      [,8]      [,9]
# [1,] 0.2655087 0.06178627 0.3800352 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
# [2,] 0.3721239 0.20597457 0.7774452 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
# [3,] 0.5728534 0.17655675 0.9347052 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
# [4,] 0.0000000 0.00000000 0.0000000 0.4820801 0.4112744 0.7323137 0.0000000 0.0000000 0.0000000
# [5,] 0.0000000 0.00000000 0.0000000 0.5995658 0.8209463 0.6927316 0.0000000 0.0000000 0.0000000
# [6,] 0.0000000 0.00000000 0.0000000 0.4935413 0.6470602 0.4776196 0.0000000 0.0000000 0.0000000
# [7,] 0.0000000 0.00000000 0.0000000 0.0000000 0.0000000 0.0000000 0.9128759 0.8753213 0.7773207
# [8,] 0.0000000 0.00000000 0.0000000 0.0000000 0.0000000 0.0000000 0.2936034 0.3390729 0.9606180
# [9,] 0.0000000 0.00000000 0.0000000 0.0000000 0.0000000 0.0000000 0.4590657 0.8394404 0.4346595

Based on suggestion from @AndrewGustar, another option is:

m * kronecker(diag(3), matrix(rep(TRUE,9),3))

Explanation:

#  kronecker(diag(3), matrix(rep(TRUE,9),3)) will return matrix as bellow. 
#  Which then can be used to select items from `m`
#      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
# [1,]    1    1    1    0    0    0    0    0    0
# [2,]    1    1    1    0    0    0    0    0    0
# [3,]    1    1    1    0    0    0    0    0    0
# [4,]    0    0    0    1    1    1    0    0    0
# [5,]    0    0    0    1    1    1    0    0    0
# [6,]    0    0    0    1    1    1    0    0    0
# [7,]    0    0    0    0    0    0    1    1    1
# [8,]    0    0    0    0    0    0    1    1    1
# [9,]    0    0    0    0    0    0    1    1    1

答案 1 :(得分:0)

另一种选择是使用bdiag

中的Matrix
library(Matrix)
res <- bdiag(rep(list(matrix(1, 3, 3)), 3)) * m
as.matrix(res)
#            [,1]       [,2]      [,3]      [,4]      [,5]      [,6]      [,7]      [,8]      [,9]
# [1,] 0.09393471 0.04663503 0.6971900 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
# [2,] 0.55225375 0.31951313 0.9691335 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
# [3,] 0.72516981 0.23874890 0.4703824 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
# [4,] 0.00000000 0.00000000 0.0000000 0.4625108 0.1475167 0.7445318 0.0000000 0.0000000 0.0000000
# [5,] 0.00000000 0.00000000 0.0000000 0.8562791 0.4865431 0.4850003 0.0000000 0.0000000 0.0000000
# [6,] 0.00000000 0.00000000 0.0000000 0.4659157 0.2461313 0.1413707 0.0000000 0.0000000 0.0000000
# [7,] 0.00000000 0.00000000 0.0000000 0.0000000 0.0000000 0.0000000 0.5268515 0.8442949 0.4083216
# [8,] 0.00000000 0.00000000 0.0000000 0.0000000 0.0000000 0.0000000 0.1203445 0.3223247 0.4844081
# [9,] 0.00000000 0.00000000 0.0000000 0.0000000 0.0000000 0.0000000 0.5068240 0.7421421 0.7007826
相关问题