If statement display problem

16 ビュー (過去 30 日間)
Anna
Anna 2017 年 4 月 1 日
コメント済み: Star Strider 2017 年 4 月 1 日
Hello, I am checking if all values of a vector Fr are within a certain range as shown:
% code as follows:
for i = 1:1:length(loop)
if Fr(i) > 0.1 && Fr(i) < 0.3
disp('0.10 < Fr < 0.30. Criteria met.')
else
disp(' Fr outside interval. Criteria not met.')
end
end
Here Fr is a vector with length(loop) values. I want the code to show the message only once if all the elements of Fr are within the range. If not, the message for Fr outside the interval should also only be shown once.
In other words, when running the code I want only one message to be displayed. Either all values are within the interval or they are not.
Is there a way of doing this?

採用された回答

Star Strider
Star Strider 2017 年 4 月 1 日
Use the all function:
Fr = 0.0001*rand(1,10)+0.2; % Create ‘Fr’
if all((Fr > 0.1) & (Fr < 0.3))
disp('0.10 < Fr < 0.30. Criteria met.')
else
disp(' Fr outside interval. Criteria not met.')
end
Change some of the elements of ‘Fr’ her to test it. Your code works with this small tweak.
  2 件のコメント
Anna
Anna 2017 年 4 月 1 日
Thank you very much!
Star Strider
Star Strider 2017 年 4 月 1 日
As always, my pleasure!

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

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2017 年 4 月 1 日
No loop.
if all(Fr>0.1 & Fr<0.3)

カテゴリ

Help Center および 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