Why is my code running so slowly in the while loop?

5 views (last 30 days)
Hi, I am using MATLAB R2020a on a MacOS. I am currently trying to calculate the exponentially weighted moving mean by applying a weight to a vector of values using the DSP signal porcessing toolbox algorithm, but it keeps stalling at this point within the while loop without actually giving an error. I am not sure how to solve this without having to preallocate an array outside the loop for storage reasons. Any suggestions would be highly appreciated. Thanks in advance.
while currentcycle > 1
current_expmean_v = (1 - 1/weight(currentcycle))*(previous_expmean_v) + (1/weight(currentcycle))*(values_v);
current_expmean_w = (1 - 1/weight(currentcycle))*(previous_expmean_w) + (1/weight(currentcycle))*(values_w);

Accepted Answer

Paul Hoffrichter
Paul Hoffrichter on 26 Nov 2020
Edited: Paul Hoffrichter on 26 Nov 2020
>>> while currentcycle > 1
currentcycle is not changing within the while-loop. You need to modify the value of currentcycle in the loop.
Paul Hoffrichter
Paul Hoffrichter on 26 Nov 2020
If the value of currentcycle is 2 when it enters the loop, then the expression "currentcycle > 1" is true, and so just focus on length(diff(qrs_i_raw). Since you say you are getting stuck, then it must be that
2 <= length(diff(qrs_i_raw))
Since qrs_i_raw is not changing its value or its length within the while-loop, its length is constant within the while-loop, so the test conditions remain true, and you are looping forever.
You have to make sure that the conditions you test for in the while-loop can change so that eventually the test condition will become false.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by