simplify my script?

2 views (last 30 days)
Den
Den on 28 Jun 2022
Edited: Jan on 29 Jun 2022
hello, I need some suggestion
I know my code isn't perfect, but this is enough for me. just wondering if my code could be more compact or simpler. the point is the same only different in the grouping per 10 days. I only want to run my code once. here is my code.
for n=1:12
% Load data
ncfile = 'Temperature.nc';
months = {'January', 'February', 'March', 'April', 'May', 'June', ...
'July', 'August', 'September', 'October', 'November', 'December'};
% to read a variable 'var' exisiting in nc file
lon = ncread(ncfile,'longitude');
lat = ncread(ncfile,'latitude');
tempI = ncread(ncfile,'t2m');
% grup date
startDate = datetime(2021,1,1,0,0,0);
endDate = datetime(2021,12,31,23,0,0);
dates = startDate:hours(1):endDate;
%% pick each 10 days data
Onemonth = dates.Month==n ;
t = dates(Onemonth) ;
Ddays = t.Day<=10 ; %date 1-10
% Ddays = t.Day >= 11 & t.Day <=20 ; % date 11-20
% Ddays = t.Day >= 21 & t.Day <=31 ; % date 21-31
%%
% Extract data per month
t10days = tempI(:,:,Ddays);
% rotate data
tempII = flipud(rot90(t10days));
% Average data
temp = mean((tempII-273.15),3);
% Plot
figure
imagescn(lon,lat,temp);
colormap jet
axis image
end
for n=1:12
% Load data
ncfile = 'Temperature2m.nc';
months = {'January', 'February', 'March', 'April', 'May', 'June', ...
'July', 'August', 'September', 'October', 'November', 'December'};
% to read a variable 'var' exisiting in nc file
lon = ncread(ncfile,'longitude');
lat = ncread(ncfile,'latitude');
tempI = ncread(ncfile,'t2m');
%% grup date
startDate = datetime(2021,1,1,0,0,0);
endDate = datetime(2021,12,31,23,0,0);
dates = startDate:hours(1):endDate;
%% pick each 10 days data
Onemonth = dates.Month==n ;
t = dates(Onemonth) ;
% Ddays = t.Day<=10 ; %date 1-10
Ddays = t.Day >= 11 & t.Day <=20 ; % date 11-20
% Ddays = t.Day >= 21 & t.Day <=31 ; % date 21-31
%%
% Extract data per month
t10days = tempI(:,:,Ddays);
% rotate data
tempII = flipud(rot90(t10days));
% Average data
temp = mean((tempII-273.15),3);
%% Plot
figure
imagescn(lon,lat,temp);
colormap jet
axis image
end
for n=1:12
%% Load data
ncfile = 'Temperature2m.nc';
months = {'January', 'February', 'March', 'April', 'May', 'June', ...
'July', 'August', 'September', 'October', 'November', 'December'};
% to read a variable 'var' exisiting in nc file
lon = ncread(ncfile,'longitude');
lat = ncread(ncfile,'latitude');
tempI = ncread(ncfile,'t2m');
%% grup date
startDate = datetime(2021,1,1,0,0,0);
endDate = datetime(2021,12,31,23,0,0);
dates = startDate:hours(1):endDate;
%% pick each 10 days data
Onemonth = dates.Month==n ;
t = dates(Onemonth) ;
% Ddays = t.Day<=10 ; %date 1-10
% Ddays = t.Day >= 11 & t.Day <=20 ; % date 11-20
Ddays = t.Day >= 21 & t.Day <=31 ; % date 21-31
%%
% Extract data per month
t10days = tempI(:,:,Ddays);
% rotate data
tempII = flipud(rot90(t10days));
% Average data
temp = mean((tempII-273.15),3);
%% Plot
figure
imagescn(lon,lat,temp);
colormap jet
axis image
end
  3 Comments
Dyuman Joshi
Dyuman Joshi on 28 Jun 2022
See Jan's answer.

Sign in to comment.

Answers (1)

Jan
Jan on 28 Jun 2022
Move all repeated work out of the loops. In your case:
% Load data
ncfile = 'Temperature.nc';
months = {'January', 'February', 'March', 'April', 'May', 'June', ...
'July', 'August', 'September', 'October', 'November', 'December'};
% to read a variable 'var' exisiting in nc file
lon = ncread(ncfile,'longitude');
lat = ncread(ncfile,'latitude');
tempI = ncread(ncfile,'t2m');
% grup date
startDate = datetime(2021,1,1,0,0,0);
endDate = datetime(2021,12,31,23,0,0);
dates = startDate:hours(1):endDate;
Do this once before all loops, because it does not depend on the loop counter n.
  3 Comments
Jan
Jan on 29 Jun 2022
Edited: Jan on 29 Jun 2022
Ddays = (t.Day >= Day_range(i_day,1) & t.Day <= Day_range(i_day,2));
This replies a logical vector, which is TRUE, if the element of t.Day is inside the specified range.
for i_day = 1:size(Day_range,1)
Of course, if the corresponding size equal 3, you can write 3 also. But this approach is more general and determines the size dynamically. It is a good programming practice to write flexible code without assumptions about the inputs.
Replace
flipud(rot90(t10days));
by the more direct:
permute(t10days, [2, 1, 3]);
Or even better:
t10days = tempI(:,:,Ddays);
tempII = flipud(rot90(t10days));
temp = mean((tempII-273.15),3);
by the cheaper and simpler:
temp = mean(tempI(:, :, Ddays), 3).' - 273.15;

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by