choose samples in for loop

1 回表示 (過去 30 日間)
NIKHIL VALSAN KULANGARETH
NIKHIL VALSAN KULANGARETH 2018 年 3 月 19 日
回答済み: Akira Agata 2018 年 3 月 20 日
consider the for loop below, for k=1:length(X)
if(X(k)>50)
count=count+1;
end
In the above case X(k) will be evaluated and condition is checked for all the values of k. I don't want to check it for all values of k, instead like every 10th or 15thh samples will do.
What should be done in this case?

回答 (2 件)

David Fletcher
David Fletcher 2018 年 3 月 20 日
You could use a step in your for statement i.e.
for k=1:10:length(X)
  1 件のコメント
NIKHIL VALSAN KULANGARETH
NIKHIL VALSAN KULANGARETH 2018 年 3 月 20 日
thanks a lot

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


Akira Agata
Akira Agata 2018 年 3 月 20 日
Also, you don't need to use for-loop. For example, if you want to do that every 10th samples, you can calculate count easily by:
idx = X(1:10:end) > 50;
count = count + nnz(idx);

カテゴリ

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