Slow performance using polyfit on large arrays - how to speed up?
2 次查看(过去 30 天)
显示 更早的评论
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
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.
采纳的回答
更多回答(1 个)
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!