displaying the total number of times an if statement is true

10 ビュー (過去 30 日間)
C.G.
C.G. 2020 年 11 月 20 日
コメント済み: C.G. 2020 年 11 月 20 日
I have a for loop with an if/else loop nested inside this:
xc = particledata{1}(:,5);
xc_array = table2array(xc);
for b = 1:size(xc_array,1)
if xc_array(b,1)>0.188
disp('grain left rice pile')
end
end
The code works, and it displays 'grain left rice pile' if the value is >0.188. For the particlaur file, it should find 29 numbers >0.188.
I would like to write some code which would tell me how many times matlab has found a number >0.188 in the array instead of me counting how many times those words are displayed in the command window.
Could anybody help me with this?

採用された回答

Rik
Rik 2020 年 11 月 20 日
You can use the fact that sum will functionally cast your logical array to 0 and 1:
grains=sum(xc_array(:,1)>0.188);
fprintf('%d grain(s) left on rice pile\n',grains);

その他の回答 (0 件)

カテゴリ

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