Filter löschen
Filter löschen

For loop for multiple vectors

2 Ansichten (letzte 30 Tage)
maria
maria am 17 Apr. 2015
Bearbeitet: Star Strider am 17 Apr. 2015
Hello people:
I have the following code:
L=500;
S=ones(1,L);
n=50;
S((L/2-n(i)/2):(L/2+n(i)/2))=0;
I would like to have a matrix M(10,L) where M(1,L) is S when n=50, M(2,L) is S when n=100 and M(3,L) is n=150 and so on...
Does anyone knows how to do it using a loop?
Thank you,
Maria

Akzeptierte Antwort

Star Strider
Star Strider am 17 Apr. 2015
Bearbeitet: Star Strider am 17 Apr. 2015
With only three iterations, the easiest way is to use a for loop:
L=500;
S=ones(1,L);
M = zeros(10,L);
n = [50, 100, 150];
for i = 1:length(n)
S((L/2-n(i)/2):(L/2+n(i)/2))=0;
M(i,:) = S;
end
EDIT — Inserted ‘M’ preallocation.

Weitere Antworten (0)

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