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

2 visualizzazioni (ultimi 30 giorni)
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 ];

Risposta accettata

Voss
Voss il 25 Mar 2022
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

Più risposte (0)

Prodotti


Release

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by