How to create a block diagonal matrix from the slices of a 3D matrix

4 ビュー (過去 30 日間)
Mohammed
Mohammed 2014 年 10 月 2 日
コメント済み: Yunsong Liu 2019 年 7 月 18 日
I have a large 3D matrix (K) composed of 200 slices , and I want to create a block diagonal matrix from the slices of it without looping. For example
K(:,:,1) =[1 2;3 4]
K(:,:,2) =[5 6;7 8]
K(:,:,3) =[9 10;11 12]
.
.
.
K(:,:,200) =[1 10;13 15]
if we assume the number of slice is 3, the output should be
1 2 0 0 0 0
3 4 0 0 0 0
0 0 5 6 0 0
0 0 7 8 0 0
0 0 0 0 9 10
0 0 0 0 11 12
But how the block diagonal matrix created from all the 200 slices without looping?

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 10 月 2 日
k(:,:,1) =[1 2;3 4]
k(:,:,2) =[5 6;7 8]
k(:,:,3) =[9 10;11 12]
q=num2cell(k,[1,2])
blkdiag(q{:})
  4 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2014 年 10 月 2 日

هذه إحدى التقنيات تعلمتها في هذا المنتدى

Matt J
Matt J 2014 年 10 月 2 日
Note that this still uses a for-loop inside num2cell.m, though how much that hurts you, I don't know.
Note also that my 2nd answer here was the same as this, except that it returns the result in sparse form, which is normally more desirable for block diagonal matrices with small blocks.

サインインしてコメントする。

その他の回答 (3 件)

Matt J
Matt J 2014 年 10 月 2 日
編集済み: Matt J 2014 年 10 月 2 日
I doubt that avoiding loops leads to the fastest code, but the following does avoid them,
[m,n,p]=size(K);
BlockMatrix=kron(speye(p), ones(m,n));
BlockMatrix(logical(BlockMatrix))=K(:);
  2 件のコメント
Mohammed
Mohammed 2014 年 10 月 2 日
thank for posting, but where is the output matrix
Azzi Abdelmalek
Azzi Abdelmalek 2014 年 10 月 2 日
The output is
full(BlockMatrix)

サインインしてコメントする。


Matt J
Matt J 2014 年 10 月 2 日
編集済み: Matt J 2014 年 10 月 2 日
This doesn't avoid a loop, even if it looks like it does, but it might be the fastest way.
Kcell = cellfun(@sparse, num2cell(K,[1,2]), 'uni',0 );
BlockMatrix=blkdiag(Kcell{:});

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 10 月 2 日
k(:,:,1) =[1 2;3 4]
k(:,:,2) =[5 6;7 8]
k(:,:,3) =[9 10;11 12]
out=[];
for ii=1:size(k,3)
out=blkdiag(out,k(:,:,ii))
end
  1 件のコメント
Mohammed
Mohammed 2014 年 10 月 2 日
Thank for posting your answer, but I need an answer without loops

サインインしてコメントする。

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by