converting for loop to matrix operation
Show older comments
Hello All I wondering if I can do the following calculation without using for loop I am using function called daily_harmonic_regression which takes one vector data, that the reason that I use for loop. For big matrix, such calculation takes a lot of time. msl is (length(long),length(lat), length(time))
anom_mslp=msl;
for lo=1:length(long);
for la=1:length(lat)
[anom,cycle_mslp]=daily_harmonic_regression(squeeze(msl(lo,la,:)));
anom_mslp(lo,la,:)=anom;
end
end
thanks in advance
4 Comments
Sara
on 29 May 2014
You'll need to post the function daily_harmonic_regression to get help
ahmed shaaban
on 29 May 2014
dpb
on 29 May 2014
function [anom,cycle]=daily_harmonic_regression(data)
t=2*pi*(1:length(data))/365.25.';
sint=sin(t);
cost=cos(t);
sin2t=sin(2*t);
etc., ...
X=[ones(length(data),1) sint cost sin2t cos2t sin3t cos3t sin4t cos4t];
C=inv(X'*X)*X';
Up to here for a given array everything is redundant for every column so factor this out into another routine that returns C. Then you can write a second solver routine the applies it to each column of data.
BTW, the inverse of the normal equations is a pretty numerically marginal solution technique; may want to look at Matlab '\' operator.
ahmed shaaban
on 2 Jun 2014
Accepted Answer
More Answers (0)
Categories
Find more on MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!