Slow performance using polyfit on large arrays - how to speed up?

1 回表示 (過去 30 日間)
Emil
Emil 2015 年 10 月 16 日
回答済み: Dennie 2015 年 10 月 16 日
I have two large arrays PatlakX & PatlakY where I perform polyfit for each row in a for loop (Matlab2015b). The problem is the slow performance of polyfit in a for loop. Any good tips how to speed up?
Currently it takes around 20 min to complete.
%code below
PatlakX = 11337728x6 double
PatlakY = 11337728x6 double
for x=1:length(PatlakX);
P = fast_polyfit(PatlakX(x,:),PatlakY(x,:),1)%
k(x,:) = P(1);
m(x,:) = P(2);
r2(x,:) = rsquare(PatlakY(x,:),polyval(P,PatlakX(x,:)));
end
Best Regards
Emil
  2 件のコメント
Walter Roberson
Walter Roberson 2015 年 10 月 16 日
You do not appear to be using polyfit: you appear to be using fast_polyfit, which is not a Mathwork supplied routine.
Emil
Emil 2015 年 10 月 16 日
It is a slightly modified polyfit with some unnecessary code removed (checkpoints) in order to speed up. Forgot to mention that modification.

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

採用された回答

Walter Roberson
Walter Roberson 2015 年 10 月 16 日

その他の回答 (1 件)

Dennie
Dennie 2015 年 10 月 16 日
I don't believe the problem is that the for loop itself is slow. However, you have a tremendous amount of loops.
If the operation takes 20 min, that means that each loop takes around 0.1 ms (10 kHz).
It seems to me like you are making a linear fit of 6 points, you can also do this without polyfit and just make a simplified matrix operation out of this. Although I am not sure if this will be faster.
a=(PatlakX(:,6)-Patlakx(:,1))./(Patlaky(:,6)-Patlaky(:,1));
b= Patlaky(:,1)-a.*Patlakx(:,1);
This will give you the values for y=ax+b.
Ofcourse you could extend the linearization of the matrix to more complex models that average the slope of the 6 points, this was just an example.

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by