while loop performance improvement

Hello everyone,
What is the alternative of while loop for the example:
rate = 2;
while (k <= period)
sum = sum + array(k,2);
k = k + rate;
end
Thanks.

 採用された回答

Fabio Freschi
Fabio Freschi 2019 年 11 月 17 日

1 投票

I don't know the starting value of k, assuming it is 1
idx = 1:rate:period
mysum = sum(array(idx,2)); % note that sum is the name of a matlab function

1 件のコメント

cglr
cglr 2019 年 11 月 18 日
編集済み: cglr 2019 年 11 月 18 日
Thank you.
maxDuration = 0;
while (k <= period)
if( myArray (k,2) > maxDuration)
maxDuration = myArray (k,2);
end
k = k + rate;
end
If I make it more complex with if statement like that, how can I use that idea ?

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

その他の回答 (0 件)

カテゴリ

質問済み:

2019 年 11 月 17 日

編集済み:

2019 年 11 月 18 日

Community Treasure Hunt

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

Start Hunting!

Translated by