How to use blkdiag command to create a matrix such that its diagonal blocks are random 2 x 2 matrices?

for k = 1 : 4
A = rand(2,2)
end
By using the above command I got 4 random matrices.How to create a block diagonal matrix of the form
a 0 0 0
0 b 0 0
0 0 c 0
0 0 0 d
(Size of this matrix will be 8x8.)
such that a is the first random 2x2 matrix , b is the second random 2x2 matrix and so on.
how to use blkdiag command so that I can get a 8x8 r block diagonal matrix with 4 random matrices as their diagonal blocks.
Thanks

 채택된 답변

Matt J
Matt J 2019년 3월 18일
편집: Matt J 2019년 3월 18일
Acell=mat2cell(rand(8,2), ones(1,4)*2,2)
B=blkdiag(Acell{:})

댓글 수: 3

Hey,
thank you so much.
Can u please tell how to write this in a for loop .. i mean how to create a NxN block diagonal matrix and with its blocks of size mxm where m<N.
You can use a loop to fill a cell array
for i=1:4
Acell{i}=...
end

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Operating on Diagonal Matrices에 대해 자세히 알아보기

질문:

2019년 3월 18일

댓글:

2019년 3월 18일

Community Treasure Hunt

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

Start Hunting!

Translated by