MATLAB Answers

How to Reduce Computation Time

21 views (last 30 days)
Tez
Tez on 22 Aug 2017
Edited: per isakson on 23 Aug 2017
Hello,
I am new to matlab. I am analyzing satellite data.each variable is about 400x31000 in dimension. After selecting data in those variable which i want i end up with a matrix of same dimension where data is in bunches , that is there will be data in some columns. i want the mean of these bunches separately.I wrote a code to do it but it takes more than 3 hours to run.I need help to reduce the time.
q=1;
nover1=NaN(size(nover));
nover2=NaN(size(nover));
nover3=NaN(size(nover));
meantime2=NaN(size(mean_ctime));
date2=NaN(size(date));
mlat1=NaN(size(mean_lat));
mlat2=NaN(size(mean_lat));
mlon1=NaN(size(mean_lon));
mlon2=NaN(size(mean_lon));
for i=1:size(alt,1);
for j=1:size(alt,2);
if alt(i,j)>80 && alt(i,j)<100;
if mean_lat(1,j)>0 && mean_lat(1,j)<20;
mlat1(1,j)=mean_lat(1,j);
mlon1(1,j)=mean_clon(1,j);
nover1(i,j)=nover(i,j);
meantime1(1,j)=mean_ctime(1,j);
date1(1,j)=date(1,j);
end
end
end
end
for j=1:size(alt,2);
if mean_clon(1,j)>60 && mean_clon(1,j)<120;
mlat2(1,j)=mlat1(1,j);
mlon2(1,j)=mlon1(1,j);
nover2(:,j)=nover1(:,j);
meantime2(1,j)=meantime1(1,j);
date2(1,j)=date1(1,j);
else
nover2(nover2==0)=NaN;
meantime2(meantime2==0)=NaN;
date2(date2==0)=NaN;
nno=nonzeros(nover2);
ntime=nonzeros(meantime2);
ndate=nonzeros(date2);
mno_42(1,q)=nanmean(nno); % the final no ver values
mtime_42(1,q)=nanmean(ntime); % the final time values
mdate_42(1,q)=nanmean(ndate); % the final date values
q=q+1;
nover2=NaN(size(nover));
meantime2=NaN(size(mean_time));
date2=NaN(size(date));
end
end
  2 Comments
Tez
Tez on 22 Aug 2017
netcdf files

Sign in to comment.

Answers (1)

per isakson
per isakson on 22 Aug 2017
Edited: per isakson on 23 Aug 2017
The code analyzer box in the upper right corner tells you that there is potential for improving the code. Hovering over the small bars in the right column will give you hints on how.
"preallocation for speed" see: Preallocation. That alone will work wonders on the speed of your code.
If that isn't enough see Profile to Improve Performance
Without being able to run the code it is difficult to be more specific than this.

Community Treasure Hunt

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

Start Hunting!

Translated by