When a variable (x) exeed the limit value (y) doing so after 5 times then stop the analysis.

1 件のコメント

Dyuman Joshi
Dyuman Joshi 2023 年 5 月 22 日
Are you using a loop to update the variable (x) or check if x>y? If so, then you can use break.

サインインしてコメントする。

 採用された回答

Image Analyst
Image Analyst 2023 年 5 月 22 日

0 投票

Try something like this
exceedCount = 0;
for k = 1 : 1000000
% First update x in the loop. Then compare it to y.
if x > y
% x is greater than y.
% Increment the count of how many times this has happened.
exceedCount = exceedCount + 1;
% Quit loop if x has exceeded y 5 times.
if exceedCount >= 5
break;
end
end
end

7 件のコメント

Chaudhary P Patel
Chaudhary P Patel 2023 年 5 月 25 日
Sir, my problem is like this so suggest me where i should use the exceedCount.
for K=1:10000
%u0 calculated from some loop
%ut input limit value
%uc input limit value
if u0<ut && u0<uc
no problem for exceedCount
elseif u0>ut
exceedCount calculated
else
exceedCount calculated
end
end
Chaudhary P Patel
Chaudhary P Patel 2023 年 5 月 25 日
@Image Analyst sir, please suggest me.
Image Analyst
Image Analyst 2023 年 5 月 25 日
Not really sure what you want to do, or how your code checks for the "5 times" criteria, but your code can be simplified to this:
for K=1:10000
%u0 calculated from some loop
%ut input limit value
%uc input limit value
if (u0>ut) || ~(u0<ut && u0<uc)
exceedCount = exceedCount + 1
end
end
but this is just your code simplified. I don't know if it's right or not. I suspect it's not. Which of the ut or uc is the upper limit and which is the lower limit?
Maybe it should be
for K=1:10000
%u0 calculated from some loop
%ut upper limit value
%uc lower limit value
if (u0>ut) || (u0<uc)
% Out of acceptable range.
exceedCount = exceedCount + 1
end
end
What I originally gave is what I believe would be right given your original desciption of the problem.
Chaudhary P Patel
Chaudhary P Patel 2023 年 5 月 25 日
Sir @Image Analyst, when i am comparing the u0 with ut and uc,
There are three different case in first case when u0<ut && u0>uc then no need to count the exceed.
But for the second case occured that is u0>ut and u0<uc then we have to count the exceed and this exceed is allwoed for five time after that if it is exceeding then we have to break the analysis.
Image Analyst
Image Analyst 2023 年 5 月 25 日
So do this:
exceedCount = 0;
for k = 1 : 1000000
% First update x in the loop. Then compare it to y.
if (u0 > ut) || (u0 < uc)
% u0 is out of the acceptable range.
% Increment the count of how many times this has happened.
exceedCount = exceedCount + 1;
% Quit loop if x has exceeded y 5 times.
if exceedCount >= 5
break;
end
end
end
In the else/otherwise case, the exceedCount is not incremented. If you want, you can set the count back to zero in that case. It just depends on if you want to check on 5 times overall, even if there are gaps, or if you want the 5 times to be in adjacent elements.
Chaudhary P Patel
Chaudhary P Patel 2023 年 5 月 26 日
Sir @Image Analyst, for my analsis requirement i have to consider the u0<ut && u0>uc for the same loop using elseif condition.
Then where i should use the exceedCount?
Image Analyst
Image Analyst 2023 年 5 月 26 日
Seems inefficient, but you can do
exceedCount = 0;
for k = 1 : 1000000
% First update x in the loop. Then compare it to y.
if (u0 < ut) && (u0 > uc) % Signal is above min and below max.
% u0 is in the acceptable range.
% Do whatever.
else
% u0 is out of the acceptable range.
% Increment the count of how many times this has happened.
exceedCount = exceedCount + 1;
% Quit loop if x has exceeded y 5 times.
if exceedCount >= 5
break;
end
end
end
To learn other fundamental concepts, invest 2 hours of your time here:

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by