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

Dyuman Joshi
Dyuman Joshi 2022년 6월 28일
The 3 loops are identical except for the data file, even then 2nd and 3rd loop loads the same file.
What is your changing variable in all the codes? The file?
You can make your code a function file and call accordingly.
Den
Den 2022년 6월 28일
yes, all use the same file. therefore I want to make it more compact.
the only difference is the date. first code 1-10, second code 11-20, and 21-31.
I'm new to matlab. just in my understanding, function is used for repetitive for different files. I only use this code once. I just want to know if there is a way to simplify my code
Dyuman Joshi
Dyuman Joshi 2022년 6월 28일
See Jan's answer.

댓글을 달려면 로그인하십시오.

 채택된 답변

Jan
Jan 2022년 6월 28일

0 개 추천

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

Moreover, the only thing that changes in your loop is the Ddays line, you could define your range of days before the loop and loop for all your wanted ranges:
%Create day_range arrays, first column is low value second is high; each
%row is a new range of selected days
Day_range = [1,10; 11, 20; 21, 31]
Day_range = 3×2
1 10 11 20 21 31
for i_day = 1:size(Day_range,1)
for n = 1:12
%% pick each 10 days data
Onemonth = dates.Month==n ;
t = dates(Onemonth) ;
Ddays = t.Day >= Day_range(i_day,1) & t.Day <= Day_range(i_day,2) ;
% Extract data per month
t10days = tempI(:,:,Ddays);
% rotate data
tempII = flipud(rot90(t10days));
% Average data
temp = mean((tempII-273.15),3);
%% Plot
figure(i_day) %new figure for each range
imagescn(lon,lat,temp);
colormap jet
axis image
end
end
Den
Den 2022년 6월 28일
Yes thank you. this is the answer i want.
although I do not understand the logic in this line.
Ddays = t.Day >= Day_range(i_day,1) & t.Day <= Day_range(i_day,2) ;
but I have matched and the answer is the same.
and if you have time to answer, why use this
for i_day = 1:size(Day_range,1)
instead of like this
for i_day = 1:3
What is the difference?
Jan
Jan 2022년 6월 29일
편집: Jan 2022년 6월 29일
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;

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 White에 대해 자세히 알아보기

제품

질문:

Den
2022년 6월 28일

편집:

Jan
2022년 6월 29일

Community Treasure Hunt

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

Start Hunting!

Translated by