How to create a matrix with special block diagonal structure
Mostrar comentarios más antiguos
I would like to create a matrix that has the following structure ( The blue dots are ones and the rest is zeros)

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?
Respuesta aceptada
Más respuestas (3)
n = 8;
c = cell(1, n);
for k = 1:n
c{k} = tril(ones(k));
end
B = blkdiag(c{:});
spy(B)
Torsten
el 25 de Mayo de 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
N = 8;
C = arrayfun(@ones,1:N,'uni',0);
B = tril(blkdiag(C{:}));
spy(B)
Categorías
Más información sobre Resizing and Reshaping Matrices en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


