How to create diagonal block matrix
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천

I am trying to write code for the above matrix. I am very new to MATLAB programming. If anyone can help me in someway, It will be very nice. Thank you.
채택된 답변
Tommy
2020년 4월 9일
How about this?
>> blkdiag(reshape(1:9,3,3)', [1,2;4,5], [1,2;4,5], 1, 1)
ans =
1 2 3 0 0 0 0 0 0
4 5 6 0 0 0 0 0 0
7 8 9 0 0 0 0 0 0
0 0 0 1 2 0 0 0 0
0 0 0 4 5 0 0 0 0
0 0 0 0 0 1 2 0 0
0 0 0 0 0 4 5 0 0
0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 0 1
댓글 수: 8
what is I want this to be in a generalized form? I have given just one example. I am trying to generalize this type of matrix. For example the first block could be of 4*4 dimension and the second block might repeat itself 3 times rather just 2. How can I do that?
c = input('c');
g = input('g');
Q = zeros(c,1);
t = 1 : c ;
ct = length( t ) ;
a_t = cell( ct, 1 ) ;
for i = 1:c+1
Q = sym('Q%d', [c+1 1]);
end
A = diag(Q)
BigA = zeros(c,1);
for j = 1:c
a_t = min(j,c-g);
ACell = repmat({A}, 1, a_t)
end
BigACell = blkdiag({ACell{:}})
B=blkdiag(BigACell{:});
This is my code...I want to make a big diagonal matrix from the output of 3 different matrics..B should be big block diagonal matrix with 3 different BigACell output matrix but I am unable to do this. Please help me here.
Tommy
2020년 4월 9일
Hmm... can you explain what c and g are and how they relate to the final block diagonal matrix? And you are hoping to use symbolic variables?
raina RAJ
2020년 4월 10일
c and g are integers and g<c. And please ignore the symbolic variables matix. I will assign them some values later on. For the final block matrix, c is the size of block matrix i.e. the size of B is c+1. i.e. B has c+1 block matrix of order c+1, c, c-1, c-2,....2,1. in the decreasing order.
Sorry, I think I'm getting closer but I'm still not quite there. What is g? Could you draw the output for c = 3 and g = 2? How about c = 3 and g = 1?
And am I correct in thinking that c = 2, for example, means B will have values in this pattern:
1 1 1 0 0 0
1 1 1 0 0 0
1 1 1 0 0 0
0 0 0 1 1 0
0 0 0 1 1 0
0 0 0 0 0 1
raina RAJ
2020년 4월 11일
I will explain in the details the whole thing. 'c' will determine the size of blocks which I already explained to you that they will be in decreasing order. 'g' is basically used in a_t = ,min{j,c-g}. Here a_t determines how many time a block should be repeated. For example in case of c=3 and g=1, first block will be of size c+1 i.e. 4 now how many times it should be repeated??? It can be determined by a_t. Here j=1, c=3, g=1 so a_t = min{1,3-1}=1. So the first block will be repeated 1 time. Now the second block. It will have dimension 'c' as I expained earlier. Now the number of times this block will be repeated is a_t = min{2,3-1}=2 since j=2 here. So the second block will be repeated 2 times. Now the third block. Again it will have size 'c-1' and the number of times it will be repeated is a_t=min{3,3-1}=2. So the third block also will be repeated 2 times. Similarly last block will have dimension 'c-2' and it will be repeated a_t = min{4,3-1}=2 times. So the output will look like something as following:
1 1 1 1 0 0
1 1 1 1 0 0
1 1 1 1 0 0
1 1 1 1 0 0
0 0 0 0 1 1 1
0 0 0 0 1 1 1
0 0 0 0 1 1 1
0 0 0 0 0 0 0 1 1 1
0 0 0 0 0 0 0 1 1 1
0 0 0 0 0 0 0 1 1 1
0 0 0 0 0 0 0 0 0 0 1 1
0 0 0 0 0 0 0 0 0 0 1 1
0 0 0 0 0 0 0 0 0 0 0 0 1 1
0 0 0 0 0 0 0 0 0 0 0 0 1 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
Thank you, that was just what I needed! Please, let me know if the following works:
c = input('c? ');
g = input('g? ');
M = cell(c+1,1);
a_t = min([1:c+1;repmat(c-g,1,c+1)]);
for i = 1:c+1
MCell = repmat({sym([char(i-1+'A') '%d%d'], [c+2-i c+2-i])}, 1, a_t(i));
M{i} = blkdiag(MCell{:});
end
B=blkdiag(M{:});
raina RAJ
2020년 4월 11일
It's perfect Thank you so much
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Operating on Diagonal Matrices에 대해 자세히 알아보기
참고 항목
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
