improve for loop performance

3 ビュー (過去 30 日間)
michael
michael 2022 年 9 月 20 日
コメント済み: michael 2022 年 9 月 20 日
Hi,
I have a for loop runing on all values and takes into acount previous value
for i=2:length(a)
a(i)=some_function(input1, input2, a(i-1))
end
where a is of size 200,000-300,000
Is there a way to improve it and speed up matlab performance?

回答 (1 件)

Walter Roberson
Walter Roberson 2022 年 9 月 20 日
In general:
  • you could get higher performance by moving the loop inside of some_function, so as to reduce the number of function calls
  • otherwise, it depends what is being calculated inside the function. The kind of code you are using is similar to what would be used to "filter" a signal, and if you can rewrite to use filt() or filtfilt() then that might be faster -- if the processing can be written in terms of derivatives or in terms of examining previous time steps. Also, if the computation is in terms of derivatives or timesteps, then Control System Toolbox transfer functions or state space models are sometimes good representations.
  • But there are certainly some computations that filter() and transfer functions are irrelevant for. There just might not be any useful speed improvement (other than running the calculation inside of one function to save on the function calls.)
  3 件のコメント
Walter Roberson
Walter Roberson 2022 年 9 月 20 日
It sounds like input1 and input2 are not constants then?
michael
michael 2022 年 9 月 20 日
Right They are the input values for current point for the hysteresis (x,y)

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

カテゴリ

Help Center および File ExchangeBlue についてさらに検索

タグ

製品


リリース

R14SP2

Community Treasure Hunt

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

Start Hunting!

Translated by