For loop for multiple vectors

2 次查看(过去 30 天)
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

采纳的回答

Star Strider
Star Strider 2015-4-17
编辑:Star Strider 2015-4-17
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by