简体   繁体   中英

Creating a sparse block diagonal matrix in Matlab

假设B是Matlab中稀疏矩阵的单元阵列,我想形成一个稀疏块对角矩阵M,其对角块是存储在B中的矩阵。最简单/最有效的方法是什么?

Use blkdiag on a comma-separated list generated from the cell array:

result = blkdiag(B{:});

For example, with

B = {sparse([1 0 0; 2 2 0; 3 3 3]), 4*speye(2)};

this produces

>> result
result =
   (1,1)        1
   (2,1)        2
   (3,1)        3
   (2,2)        2
   (3,2)        3
   (3,3)        3
   (4,4)        4
   (5,5)        4
>> full(result)
ans =
     1     0     0     0     0
     2     2     0     0     0
     3     3     3     0     0
     0     0     0     4     0
     0     0     0     0     4

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM