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

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);
end

 採用された回答

Paul Hoffrichter
Paul Hoffrichter 2020 年 11 月 26 日
編集済み: Paul Hoffrichter 2020 年 11 月 26 日

0 投票

>>> while currentcycle > 1
currentcycle is not changing within the while-loop. You need to modify the value of currentcycle in the loop.

6 件のコメント

Cai Chin
Cai Chin 2020 年 11 月 26 日
Hi, the while loop is within a for loop, so I thought that this would happen anyway?
Paul Hoffrichter
Paul Hoffrichter 2020 年 11 月 26 日
編集済み: Paul Hoffrichter 2020 年 11 月 26 日
When you enter the while-loop, if currentcycle <= 1, then the while loop will run forever, since the value of currentcycle is not changing. It remains the same for the duration of the while-loop (which in your case, is forever).
You need to have a statement:
currentcycle = <something that eventually results in a currentcycle > 1 being false>
in order to exit from the while-loop.
Cai Chin
Cai Chin 2020 年 11 月 26 日
When I add 'currentcycle = currentcycle +1' at the end of the while loop, it produces the message: 'loop index changes within the for lopp'. Is this okay? Thanks
Paul Hoffrichter
Paul Hoffrichter 2020 年 11 月 26 日
編集済み: Paul Hoffrichter 2020 年 11 月 26 日
I edited my response removing mentioning the for-loop. Just use your while-loop and keep 'currentcycle = currentcycle +1'.
What is the value of currentcycle when you enter the while-loop? If it is 0, then your loop will only run 2 times.
Cai Chin
Cai Chin 2020 年 11 月 26 日
The value of currentcycle is 2 when it enters the loop. I have now changed it to this but it still gets stuck:
while currentcycle > 1 && currentcycle <= length(diff(qrs_i_raw))
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);
end
Is it still possible for the while loop to be within the for loop since I am doing real-time analysis so the input into the while loop depends on vales generated within the for loop for a particular cycle?
Paul Hoffrichter
Paul Hoffrichter 2020 年 11 月 26 日
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.

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

その他の回答 (0 件)

カテゴリ

質問済み:

2020 年 11 月 26 日

編集済み:

2020 年 11 月 26 日

Community Treasure Hunt

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

Start Hunting!

Translated by