Getting values from a for loop when specific condition met

I am doing a loop, and want the iteration stops when condition of x = 5 however, the code terminates before x=5 ?
So any help please
clear all;
clc;
x=[1 2 3 4 5 6 7 8 9 10];
for n=1:10
if x(n)>= 5;
break
end
disp('how many iteration done now');
final(n,:)=x(n)
z(n,:)=x(n)+2
end

1 Kommentar

It doesn't calcluate anything for n=5, because you tell Matlab to stop before that. What is your question?

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

dpb
dpb am 25 Aug. 2019
break is like drawing the "Go To Jail" card in Monopoly--you "go directly to jail, do not pass GO, do not collect $200". Here when you get to the conditional and it is satisfied, then the code immediately goes to the next line of code after the terminating end of the for loop and so the other code isn't executed that last iteration--the loop did do five iterations here because it took until then for x(n) to be >=5; just that you didn't do anything at all in the loop other than the test.
Perhaps your expectations could be met simply by reordering:
...
for n=1:10
disp('how many iteration done now');
final(n,:)=x(n)
z(n,:)=x(n)+2
if x(n)>= 5, break, end
end
...
Then again, that assumes that what happens now is what you want to happen for the loop for all iterations that it does do...

6 Kommentare

Thanks, it works now, I totally understood what you meant,
Could you please try the same with While ?
In addition, which is more preferable for such problem for or while condition s?
If so, couldd you let me know how could I do the same with While.
Looking to hearing from you
Thanks,
Well, whether a counted for or a while loop is "more preferable" is often simply in the eye of the beholder--there's no hard and fast rule. But, it mostly is dependent upon how the condition is satisfied -- while makes a lot of sense if the conditional is computed as in a convergence scheme error. The for makes sense if there is a count and a lookup as you have in your example--to work through the array until the condition is met would require an external counter in while where as you "get it for free" with the for loop. Hence, it would take at least one additional step with while here which I, personally, would then think means for would be preferable.
However, for the specific case you've written, you don't need either, and the efficient use of MATLAB is to use the array syntax that is its unique feature--
final=find(x>=5,1);
z=[x(1:final)+2].';
will give the results from above with the exception that I presumed you really didn't mean to have final be an array but the actual index that satisfied the condition.
That's also awesome :)
I just did only
final=find(x>=5)
I do not understand the last command line you have written, seems for me, not correct, or not what I expect, could you please explain it to me more,
Thanks again,
You left off the second (and optional, but needed here) argument to find
Instead of me spoon-feeding, look at the documentation for find and see if you can't learn the difference between the two invocations yourself.
Going forward, it will be much more productive if you learn to read and use the doc instead of waiting for somebody here to answer a question that can be resolved thru the input descriptions.
I have already read the find doc, seems very helpful,
However, I don't agree with you in what you said about optional, or even your last code in general
(x>=5,1) 1 here means 1 which means first element that met the condition,
second, why you add the values to 2 ?
I meant, I need from x array all the numbers that met the conditions written.
Your obtained values are uncorrect.
Thanks,
dpb
dpb am 26 Aug. 2019
Bearbeitet: dpb am 26 Aug. 2019
"Optional" in that it is an optional input to find(), not that it is optional to use it in this case to find the first location in x that satisfies the condition which is what your break logic is doing.
I added 2 because that's what your code does... :)
It produced identically the same results as your code here...
>> x=1:10;
>> for n=1:10
disp('how many iteration done now');
final(n,:)=x(n);
z(n,:)=x(n)+2;
if x(n)>= 5, break, end
end
how many iteration done now
how many iteration done now
how many iteration done now
how many iteration done now
how many iteration done now
>> [final z(:,5)]
ans =
1 3
2 4
3 5
4 6
5 7
>>
Your code above w/o the intermediate results while mine produced:
>> final=find(x>=5,1);
>> z=x(1:final)+2;
>> final
final =
5
>> z.'
ans =
3
4
5
6
7
>>
with the assumption as before that there really wasn't any need for final to be an array and the desired result was that for z when the condition is met.
Now, if that isn't the result wanted, then the actual problem definition needs clarification. So, if the problem is really the statement "I meant, I need from x array all the numbers that met the conditions written." then that's just
z=x(x>=5);
using logical addressing/indexing, one of the most powerful of Matlab features. That isn't what your code actually does, but maybe that was the real question underneath the one you asked.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 25 Aug. 2019

Bearbeitet:

dpb
am 26 Aug. 2019

Community Treasure Hunt

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

Start Hunting!

Translated by