reset the counter in for loop

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

Réponses (2)

JESUS DAVID ARIZA ROYETH
JESUS DAVID ARIZA ROYETH le 16 Nov 2019
Modifié(e) : JESUS DAVID ARIZA ROYETH le 16 Nov 2019

1 vote

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 commentaires

Khalid Mamdouh
Khalid Mamdouh le 16 Nov 2019
Modifié(e) : Khalid Mamdouh le 16 Nov 2019
I am not following what this line does
sum(v<=0)
Is there a way to do this without this function ?
JESUS DAVID ARIZA ROYETH
JESUS DAVID ARIZA ROYETH le 16 Nov 2019
sum(v<=0) find the amount of values less than or equal to 0 in v
counter = 0;
for index = 1 : length(v)
if v(index) <= 0
counter = counter + 1;
end
end
solution without sum could be:
v=input('Enter an array of positive numbers:');
a=true;
while a
for k=1:length(v)
if v(k)<=0
a=true;
v=input('Error:Enter an array of positive numbers:');
break;
end
a=false;
end
end
Adam Danz
Adam Danz le 16 Nov 2019
Note that if the user presses enter without entering anything in response to input(), it will return an empty [ ] and since sum([]<=0) == 0, the user will not be prompted again. That may be desired or it may not be - just something to keep in mind.
Khalid Mamdouh
Khalid Mamdouh le 16 Nov 2019
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 le 17 Nov 2019
I have to wonder if you had accidentally created a variable named sum

Connectez-vous pour commenter.

Adam Danz
Adam Danz le 16 Nov 2019
Modifié(e) : Adam Danz le 3 Nov 2022

0 votes

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 commentaires

Walter Roberson
Walter Roberson le 16 Nov 2019
You can alter the for loop counter. However, the next time the for loop would execute, it will change the counter back.
Adam Danz
Adam Danz le 16 Nov 2019
You can override the counter variable within the loop but I was referring to the counter controlling the number of iterations. Thanks for the clarification.
Jerry Malone
Jerry Malone le 1 Nov 2022
Is this a new feature, or has Matlab always prohibited altering a for-loop counter?
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 le 3 Nov 2022
I've added a sentence to my answer to make this clearer.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by