How to create this patterned matrix?

7 次查看(过去 30 天)
How do I create a matrix that looks like this?

采纳的回答

Stephen23
Stephen23 2022-3-7
编辑:Stephen23 2022-3-7
N = 8;
M = toeplitz(0:N-1,[0,N:-1:1]);
M(N,:) = N:-1:0
M = 8×9
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 8 7 6 5 4 3 2 1 0

更多回答(2 个)

Davide Masiello
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 个评论
Burger King
Burger King 2022-3-7
I didnt even realize it was "5 6 8". Thanks a lot man
Davide Masiello
Davide Masiello 2022-3-7
Anytime! I guess you indeed wanted that row to be skipped. @Stephen's answer will do that very efficiently.

请先登录,再进行评论。


Arif Hoq
Arif Hoq 2022-3-7
编辑:Arif Hoq 2022-3-7
try this:
x=[0,8,7,6,5,4,3,2,1];
A1=circshift(x,1);
A2=circshift(x,2);
A3=circshift(x,3);
A4=circshift(x,4);
A5=circshift(x,5);
A6=circshift(x,6);
A7=circshift(x,8);
Matrix=[x;A1;A2;A3;A4;A5;A6;A7]
Matrix = 8×9
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 8 7 6 5 4 3 2 1 0

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by