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

11 visualizzazioni (ultimi 30 giorni)
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 Commenti
Walter Roberson
Walter Roberson il 16 Ott 2015
You do not appear to be using polyfit: you appear to be using fast_polyfit, which is not a Mathwork supplied routine.
Emil
Emil il 16 Ott 2015
It is a slightly modified polyfit with some unnecessary code removed (checkpoints) in order to speed up. Forgot to mention that modification.

Accedi per commentare.

Risposta accettata

Walter Roberson
Walter Roberson il 16 Ott 2015

Più risposte (1)

Dennie
Dennie il 16 Ott 2015
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.

Categorie

Scopri di più su Performance and Memory in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by