How to create a matrix with special block diagonal structure
조회 수: 1 (최근 30일)
이전 댓글 표시
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?
댓글 수: 0
채택된 답변
DGM
2022년 5월 25일
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
추가 답변 (3개)
Steven Lord
2022년 5월 25일
n = 8;
c = cell(1, n);
for k = 1:n
c{k} = tril(ones(k));
end
B = blkdiag(c{:});
spy(B)
댓글 수: 0
Torsten
2022년 5월 25일
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
Stephen23
2022년 5월 25일
N = 8;
C = arrayfun(@ones,1:N,'uni',0);
B = tril(blkdiag(C{:}));
spy(B)
댓글 수: 0
참고 항목
카테고리
Help Center 및 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!