How to create this patterned matrix?
조회 수: 7 (최근 30일)
이전 댓글 표시
How do I create a matrix that looks like this?

댓글 수: 0
채택된 답변
추가 답변 (2개)
Davide Masiello
2022년 3월 7일
편집: Davide Masiello
2022년 3월 7일
Generalizing for any nxn matrix
clear,clc
n = 9;
A = zeros(n);
for i = 1:n
A(i,i+1:n) = flip(i:n-1);
if i > 1
A(i,1:i) = flip(0:i-1);
end
end
which yields
A =
0 8 7 6 5 4 3 2 1
1 0 8 7 6 5 4 3 2
2 1 0 8 7 6 5 4 3
3 2 1 0 8 7 6 5 4
4 3 2 1 0 8 7 6 5
5 4 3 2 1 0 8 7 6
6 5 4 3 2 1 0 8 7
7 6 5 4 3 2 1 0 8
8 7 6 5 4 3 2 1 0
However, in your example, the row starting with 7 is missing. I am not sure whether that's intentional or just a typo.
In the first instance, the code above can be arranged to remove rows starting with a certain value.
댓글 수: 3
Davide Masiello
2022년 3월 7일
Anytime! I guess you indeed wanted that row to be skipped. @Stephen's answer will do that very efficiently.
참고 항목
카테고리
Help Center 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!