reset the counter in for loop

조회 수: 49 (최근 30일)
Khalid Mamdouh
Khalid Mamdouh 2019년 11월 16일
댓글: Adam Danz 2022년 11월 3일
I want to check the values in an array but when i reset the counter it ignores the new definition and continues looping. Why that happens and if there is a better way to do it please help. Thanks in advance
v=input('Enter an array of positive numbers:');
for i=1:length(v)
while v(i) <= 0
v=input('Error:Enter an array of positive numbers:');
i=1;
end
end

답변 (2개)

JESUS DAVID ARIZA ROYETH
JESUS DAVID ARIZA ROYETH 2019년 11월 16일
편집: JESUS DAVID ARIZA ROYETH 2019년 11월 16일
because the for loop once defined ignores some change in its variable and tries to execute normally, but with this you can solve your problem:
v=input('Enter an array of positive numbers:');
while sum(v<=0)>0
v=input('Error:Enter an array of positive numbers:');
end
  댓글 수: 7
Khalid Mamdouh
Khalid Mamdouh 2019년 11월 16일
Is sum(v<=0) works on 2007 version because I tried it and it prompts an error 'Index exceeds matrix dimensions'
Walter Roberson
Walter Roberson 2019년 11월 17일
I have to wonder if you had accidentally created a variable named sum

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


Adam Danz
Adam Danz 2019년 11월 16일
편집: Adam Danz 2022년 11월 3일
You cannot alter the for-loop counter. It is possible to temporarily override the value of the loop variable but it will reset to the next iteration value when the loop continues to the next iteration, though this is not advisable.
Here's an alternative that continually asks the user for a vector of positive numbers until those condtions are satisfied and then it moves on to your for-loop.
Instead of using input() it uses inputdlg() which is a bit cleaner IMO but that line can be replaced with input() if that is preferred. Additionally, it converts the char input to double (numeric).
v = NaN;
while any(isnan(v)) || any(v<0)
response = inputdlg('Enter a vector of positive numbers');
v = str2double(strsplit((response{:})));
end
for i=1:length(v)
% Do stuff
end
Example with input() instead.
v = [];
while isempty(v) || ~isnumeric(v) || any(v < 0)
v = input('Enter a vector of positive numbers: ');
end
for i=1:length(v)
% Do stuff
end
  댓글 수: 5
Steven Lord
Steven Lord 2022년 11월 1일
You can change the for loop counter. But I'm pretty sure it's always been the case that the change will only be present for the rest of the current iteration and that when the for loop enters its next iteration the loop variable will be assigned the next value from the for loop expression, overwriting the changed value. I just launched a very old release of MATLAB (release R11) and confirmed that this behavior occurs in that release. So that makes this behavior at least 22 years old. [I forget exactly when release R11 came out.]
for k = 1:10
if k == 5
k = 1;
end
disp(k)
end
1
2
3
4
1
6
7
8
9
10
The loop variable did change in the iteration when the expression gave it the value 5, but that did not "restart the loop" or anything like that. The next iteration it took the next value from the expression 1:10.
Adam Danz
Adam Danz 2022년 11월 3일
I've added a sentence to my answer to make this clearer.

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

카테고리

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