How to Reduce Computation Time
조회 수: 3 (최근 30일)
이전 댓글 표시
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
답변 (1개)
per isakson
2017년 8월 22일
편집: per isakson
2017년 8월 23일
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/177906/image.png)
"preallocation for speed" see: Preallocation. That alone will work wonders on the speed of your code.
Without being able to run the code it is difficult to be more specific than this.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Dates and Time에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!