How to create diagonal block matrix
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示

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 件のコメント
raina RAJ
2020 年 4 月 9 日
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?
raina RAJ
2020 年 4 月 9 日
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.
Tommy
2020 年 4 月 10 日
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
Tommy
2020 年 4 月 11 日
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 についてさらに検索
参考
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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)
