while loop performance improvement

1 view (last 30 days)
cglr on 17 Nov 2019
Edited: cglr on 18 Nov 2019
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;

Accepted Answer

Fabio Freschi
Fabio Freschi on 17 Nov 2019
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 Comment
cglr on 18 Nov 2019
Edited: cglr on 18 Nov 2019
Thank you.
maxDuration = 0;
while (k <= period)
if( myArray (k,2) > maxDuration)
maxDuration = myArray (k,2);
k = k + rate;
If I make it more complex with if statement like that, how can I use that idea ?

Sign in to comment.

More Answers (0)


Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!