Create a matrix (24,72) using 'for' loop.

10 Ansichten (letzte 30 Tage)
ET-TAOUSSI mehdi
ET-TAOUSSI mehdi am 15 Jan. 2016
Kommentiert: ET-TAOUSSI mehdi am 16 Jan. 2016
How can I use the loop 'for' or other, to produce the same matrix (24,72) depicted in the figure? kind regards.

Akzeptierte Antwort

Joseph Cheng
Joseph Cheng am 15 Jan. 2016
Bearbeitet: Joseph Cheng am 15 Jan. 2016
since it is a nice simple pattern of (zeros for columns that are a multiple of 3) then:
DesiredResult = ones(24,72);
DesiredResult(:,3:3:end)=0;

Weitere Antworten (1)

Brendan Hamm
Brendan Hamm am 15 Jan. 2016
You could do this with a loop:
A = ones(24,72);
for k = 1:72
if rem(k,3) == 0
A(:,k) = 0;
end
end
or you could do this in one line:
A = repmat([1 1 0],24,72/3);
The latter is much faster and more elegant.
  1 Kommentar
ET-TAOUSSI mehdi
ET-TAOUSSI mehdi am 16 Jan. 2016
the last is a good solution, thank you Brendan Hamm.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by