필터 지우기
필터 지우기

For loop for multiple vectors

조회 수: 2 (최근 30일)
maria
maria 2015년 4월 17일
편집: Star Strider 2015년 4월 17일
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