Creating a block matrix of matrices?

조회 수: 270 (최근 30일)
jgg
jgg 2016년 5월 6일
댓글: Jon 2019년 4월 4일
I have a problem where I'm trying to create a matrix of the form
[A B 0 0; 0 A B 0; 0 0 A B; 0 0 0 A];
However, this is in block matrix notation. That means all of the elements are matrices of appropriate size so that this concatenation works. I saw the blkdiag function, but it doesn't look like it's going to work for this, because the elements overlap in certain columns.
For example, if A = [1 1] and B = [2 2] this matrix would look like:
[1 1 2 2 0 0 0 0; 0 0 1 1 2 2 0 0; 0 0 0 0 1 1 2 2; 0 0 0 0 0 0 1 1]
Does anyone have any suggestions how to do this in a parsimonious way?
  댓글 수: 2
Image Analyst
Image Analyst 2016년 5월 6일
Your second row is [0 A B 0] yet your example does not show that. Your example shows [0 0 A B 0 0], so which is it? Does it shift over by one zero, or the number of elements in A, or by the number of elements in B?
We can't give the answer you want until we know for sure which it is.
jgg
jgg 2016년 5월 6일
The zeroes in the block representation are blocks of zeros, as in block matrix notation; that was unclear. Basically, the blocks shift and the zeros just backfill as necessary based on the sizes of the blocks. See blkdiag for roughly the idea.

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

채택된 답변

Hang Qian
Hang Qian 2016년 5월 6일
Hello,
If the FOR loop is not your choice, you may consider the following:
>> A = [1 1];
>> B = [2 2];
>> C = blkdiag(A,A,A,A);
>> C(1:end-1,3:end) = C(1:end-1,3:end) + blkdiag(B,B,B)
If you’d like something slightly more general,
>> nrow=2;ncol=3;
>> A=rand(nrow,ncol);B=rand(nrow,ncol);
>> C = blkdiag(A,A,A,A);
>> C(1:end-nrow,ncol+1:end) = C(1:end-nrow,ncol+1:end) + blkdiag(B,B,B)
Regards,
- Hang Qian
  댓글 수: 1
jgg
jgg 2016년 5월 6일
편집: jgg 2016년 5월 6일
This is good, but I was hoping for a more general solution that didn't reply on the blocks being the same across rows. I guess I was hoping there was some kind of block matrix notation in Matlab.

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

추가 답변 (1개)

Mark Britten-Jones
Mark Britten-Jones 2019년 3월 14일
This is by far the easiest way to do this. Create the blocks. Create a 2-D cell array and place the blocks into the appropriate cells. And then convert to a matrix by cell2mat. I have used this where I have used loops over the cell blocks to create quite complicated matrices and you do not have to worry about the indexes at the matrix level. Bonus! See the cell2mat documenation for rules regarding permissable block sizes. (They do not all have to be of the same size.)
A = [1 1];
B = [2 2];
Z = [ 0 0]
Ccell = {A, B, Z, Z; Z, A, B, Z; Z, Z, A, B; Z, Z, Z, A};
C = cell2mat(Ccell);
  댓글 수: 1
Jon
Jon 2019년 4월 4일
Actually in the example you give it is not necessary to use the cell arrays at all. You can directly assign:
C =[A B Z Z;Z A B Z;Z Z A B;Z Z Z A]

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

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by