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.

 采纳的回答

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.
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?
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
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{:});
It's perfect Thank you so much

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Operating on Diagonal Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by