I am trying to create this matrix ; How can i do this using for loops or without loops?

1 次查看(过去 30 天)
A=[1 1 1 0 0 0 0 0 0; 1 1 1 1 1 1 0 0 0 ; 1 1 1 1 1 1 1 1 1 ];

采纳的回答

Voss
Voss 2022-3-25
Here's one way without loops (just typing the matrix out):
A=[1 1 1 0 0 0 0 0 0; 1 1 1 1 1 1 0 0 0 ; 1 1 1 1 1 1 1 1 1 ];
But I guess that's not what you're looking for.
Maybe this instead:
N = 3;
A_1 = repelem(cummax(eye(N)),1,N)
A_1 = 3×9
1 1 1 0 0 0 0 0 0 1 1 1 1 1 1 0 0 0 1 1 1 1 1 1 1 1 1
isequal(A,A_1) % check
ans = logical
1
Or this:
A_2 = zeros(N,N^2);
for ii = 1:N
A_2(ii,1:N*ii) = 1;
end
A_2
A_2 = 3×9
1 1 1 0 0 0 0 0 0 1 1 1 1 1 1 0 0 0 1 1 1 1 1 1 1 1 1
isequal(A,A_2) % check
ans = logical
1

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Blue 的更多信息

标签

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by