Multiple Variable For Loop

조회 수: 6(최근 30일)
Adam Parry
Adam Parry 2012년 7월 31일
I have so far written this for loop
for i = 1:VgStep
for start = 1:VgStep:endPoint-VgStep+1
for step = VgStep:VgStep:endPoint
Vd{j}{i} = x(start:step);
Id{j}{i} = y(start:step);
I appologise for not explaining what all the terms are, but what I need is combine all those for's into one, because I end up just getting the same value for all my Vd{j}{i} terms
I'll explain better if it is needed
  댓글 수: 4
Walter Roberson
Walter Roberson 2012년 7월 31일
John, neither start nor step are reserved words. See

댓글을 달려면 로그인하십시오.

채택된 답변

Albert Yam
Albert Yam 2012년 7월 31일
In the code posted, you do not have 'j' (this might be elsewhere in your code), in your example, not even sure why you need it.
Since you know what your step size is, you don't need to loop it, the way it is now, seems incorrect. This is what I see from your start:stop. When start > stop , you will get an empty index.
1:3, 1:6, 1:9, 4:3, 4:6, 4:9, 7:3 (error), 7:6 (error), 7:9
Do you understand how the following works?
i = 1;
for start = 1:VgStep:length(Vg) %what does this count?
Vd{i} = x(start:start+2); %try to understand the indexing here
Id{i} = y(start:start+2);
i=i+1; %how is this affected by 'start'?
  댓글 수: 1
Adam Parry
Adam Parry 2012년 7월 31일
Thank you
This is exactly what i was looking for

댓글을 달려면 로그인하십시오.

추가 답변(0개)


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!

Translated by