How can I make my MATLAB code run faster?
Ältere Kommentare anzeigen
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 Kommentare
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
am 2 Mär. 2020
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu Time Series Events finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!