How to create a matrix with special block diagonal structure
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I would like to create a matrix that has the following structure ( The blue dots are ones and the rest is zeros)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1010405/image.png)
the number of the dots is determined by the triangular sequence. For example, the above matrix was obtained with n = 8. So is there any way to program this for any arbitrary n?
0 commentaires
Réponse acceptée
DGM
le 25 Mai 2022
Here's one way. I'm sure there are plenty of others.
n = 8;
sz = sum(1:n);
A = zeros(sz);
idx0 = 1;
for k = 1:n
A(idx0:idx0+k-1,idx0:idx0+k-1) = 1;
idx0 = idx0+k;
end
A = tril(A);
imshow(A)
0 commentaires
Plus de réponses (3)
Steven Lord
le 25 Mai 2022
n = 8;
c = cell(1, n);
for k = 1:n
c{k} = tril(ones(k));
end
B = blkdiag(c{:});
spy(B)
0 commentaires
Torsten
le 25 Mai 2022
ntriangles = 4;
n = ntriangles*(ntriangles+1)/2;
A = zeros(n);
rowstart = 1;
rowend = 1;
for i = 1:ntriangles
for jrow = rowstart:rowend
A(jrow:rowend,jrow) = 1.0;
end
rowstart = rowend + 1;
rowend = rowstart + i;
end
0 commentaires
Stephen23
le 25 Mai 2022
N = 8;
C = arrayfun(@ones,1:N,'uni',0);
B = tril(blkdiag(C{:}));
spy(B)
0 commentaires
Voir également
Catégories
En savoir plus sur Operating on Diagonal Matrices dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!