How can I make my MATLAB code run faster?

3 views (last 30 days)
Hi!
I've been trying to get the rainfall a day before the start of season using this code for a certain year but it looks like my run for a certain year will took 1 week to finish. My start of season is a map with dimensions 7294 x 4855 in terms of julian dates and I have also a daily rainfall map with dimensions of 180x120 for 18 years. Can anybody help me run my code faster?Thanks!
Here's my code.
clear all
clc
close all
yr = 2001;
%Rainfall data
pathgpm = 'E:\GPM_Pinas\GPM_Annual\';
flgpm = dir([pathgpm num2str(yr) '*']);
%Start of Season data
pathseas = 'D:\Start of Season 1 Rice Area\';
flseas = dir([pathseas num2str(yr) '*']);
%Rainfal lati long
load('GPM_latlon.mat')
%Start of season lat lon
load('MOD09Q1_latlon.mat')
lon = lon';
lat = lat';
load([pathseas flseas(1).name])
reseas = ricearea(:);
for i = 1:length(reseas);
disp(i)
if isnan(reseas(i)) == 1
rain1dy(i,1)=NaN;
else
tmpdt = datevec(datenum(yr,1,1)+reseas(i)-1);
load([pathgpm num2str(yr) '.mat'])
[xx yy] = meshgrid(lon,lat);
xx = xx(:);
yy = yy(:);
%%%
%%% index lon and lat
[~,indlon] = min(abs(xx(i)-long));
[~,indlat] = min(abs(yy(i)-lati));
rain1dy(i,1) = squeeze(rainall(indlat,indlon,reseas(i)-1));
end
end
  2 Comments
Guillaume
Guillaume on 2 Mar 2020
Edited: Guillaume on 2 Mar 2020
Which version of matlab are you on that you're using old datenum and datevec?
Presumably, you're aware that since the introduction of tables (R2013b), datetime (R2014b) and timetables (R2016b) filtering data for a particular time range or location range is probably just one line of code.
It's not particularly clear what your code is doing due to the lack of comments and the fact that all the load pop unknown variables into existence but if you give us some explanation we can help you come up with better code.
Feland Dolores
Feland Dolores on 2 Mar 2020
Hi, I'm using R2014b version and just a beginner at coding. The idea is getting the rainfall amount a day before the start of season for a particular pixel. For example the value of start of season for a particular pixel is 23. The 23 corresponds to Day 23 of the year. From there, I'll be calling the Day 22 of the rainfall data. So the result will be a map of rainfall before start of season.

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 2 Mar 2020
Edited: Stephen23 on 2 Mar 2020
Read the advice given here:
In particular you need to do these steps:
0- replace this overused-by-beginners command
clear all
with
clearvars
if really required. CLEAR ALL brutally removes all cached functions from MATLAB memory, something that is most likely pointless and slows down code when those functions just have to be cached again.The clear documentation states "Calling clear all decreases code performance, and is usually unnecessary."
1- preallocate the output array:
For example:
reseas = ricearea(:)
N = numel(reseas);
rain1dy = nan(N,1); % preallocate!
for k = 1:N
... your code
rain1dy(k) = ...
end
2- move things out of the loop that do NOT change on each iteration. e.g. that meshgrid call should be moved before the loop.
3- get rid of superfluous operations, e.g. get rid of squeeze.
To avoid some basic bugs, you should also:
4- load into an output variable and access its fields, i.e.:
S = load(...);
S.whateverfield
5- join filename parts use fullfile instead of concatenating strings together.

More Answers (0)

Categories

Find more on Oceanography and Hydrology 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!