For loop for multiple vectors

10 views (last 30 days)
maria
maria on 17 Apr 2015
Edited: Star Strider on 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

Accepted Answer

Star Strider
Star Strider on 17 Apr 2015
Edited: Star Strider on 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.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!