How to stop finding a value once it has been attained? (Find function inside a loop)

조회 수: 8 (최근 30일)
Hello, I am finding a first nonzero value from the array within the while loop. However, the efficiency of my code got significantly lower as I tried to find a value from the entire array every time the code loops. Is there a way to stop finding a value once it has been found? Below is the excerpt of my code that is within the while loop.
ind_e = find(current_e(2,:),1,'first');
if exist('ind_e','var')
if k > ind_e
sur_current_e(1,k) = current_e(1,k-ind_e) - current_e(2,k);
end
end
Thank you very much.

답변 (2개)

Brendan Collins
Brendan Collins 2019년 8월 8일
SungJun,
You could try something like this. Set I to 0 outside of the loop somewhere than give it a shot.
ind_e = find(current_e(2,:),1,'first');
if I == 0
if exist('ind_e','var')
if k > ind_e
sur_current_e(1,k) = current_e(1,k-ind_e) - current_e(2,k);
I = 1;
end
end
end
This will check the variable I before running back through the loop for the rest of your while loop's iterations, but because you already found the number you want, I has been changed from 0 to 1, and the nested if will not run.
  댓글 수: 1
SungJun Cho
SungJun Cho 2019년 8월 8일
Thank you Brendan! Actually what I wanted was to somehow 'stop'
ind_e = find(current_e(2,:),1,'first');
this find command once I found the ind_e value. But thanks for your help.

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


Andrei Bobrov
Andrei Bobrov 2019년 8월 8일
Please read about break in while loop.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by