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

조회 수: 3 (최근 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 CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by