Hi
I have data for the geopotential for every 6 hours over a whole month. I want to get a mean over the data for the month. For example for January, I have 124 timesteps, each with their data, but I only want data for the whole month (1 dataset). I have tried, but I'm not sure if I've done it right? Pleas can anyone help me?
lon = ncread(filename,'longitude') ; nx = length(lon) ;
lat = ncread(filename,'latitude') ; ny = length(lat) ;
time = ncread(filename,'time') ; nt = length(time);
zmean = zeros([nx ny]);
for n = 1:nt
z = ncread(filename,'z',[1 1 nt],[nx ny 1]);
zx(:,1:ny) = z(:,ny:-1:1);
zmean = zmean + zx;
end
zmean = zmean/nt;
Here is (lon = 360x1), (lat = 181x1), (time = 124x1 int32), (z = 360x181)

 Akzeptierte Antwort

jonas
jonas am 27 Okt. 2018
Bearbeitet: jonas am 27 Okt. 2018

0 Stimmen

Use datetime with retime . Upload data for details.

6 Kommentare

Jonas Damsbo
Jonas Damsbo am 27 Okt. 2018
My data and MATLAB code: https://drive.google.com/open?id=1vztWBO61lokpz8PWkPFW60NLGId_i0E8
Jonas Damsbo
Jonas Damsbo am 27 Okt. 2018
But when should I use the retime function? In the forloop or after?
jonas
jonas am 27 Okt. 2018
Bearbeitet: jonas am 27 Okt. 2018
I will take a look at your data.
What kind of format is this?
time =
124×1 int32 column vector
700512
700518
700524
You should read all your data at the same time, each month. Then you simply put it all in a timetable and use retime. No for loop required. The only problem is that I don't know how to parse your time format. I understand that it's all from the same months, but what does e.g. 700512 mean?
Jonas Damsbo
Jonas Damsbo am 27 Okt. 2018
Sounds good!
Jonas Damsbo
Jonas Damsbo am 27 Okt. 2018
Bearbeitet: Jonas Damsbo am 27 Okt. 2018
It is from ncfile and the 'units' are hours since 1900-01-01 00:00:0.0 (dont ask me why)
jonas
jonas am 27 Okt. 2018
Bearbeitet: jonas am 27 Okt. 2018
After looking at your data I think perhaps a timetable is in fact not the way to go, as I suspect that you want to retain the gridded nature of your data. If the problem is to average the monthly data, then I think there is a very simple solution. Simply,
z = ncread(filename,'z');
z_monthly(:,:,1) = mean(z,3);
This takes the average over the third dimension (time). Then you can simply put february's data in z_monthly(:,:,2) etc... and you will end up with a lat x lon x n matrix where n is the number of months.
If the data is uniformly spaced, then I see no point in using retime. If some data is missing, however, then you'd probably want to interpolate before taking the average.
I would also suggest you take your time-vector and convert it to datetime, to make life easier for you.
t = datetime(1900,1,1,0,0,0)+hours(time);
t =
124×1 datetime array
01-Dec-1979 00:00:00
01-Dec-1979 06:00:00
01-Dec-1979 12:00:00
Did I understand the issue?

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Jonas Damsbo
Jonas Damsbo am 27 Okt. 2018

0 Stimmen

I think I understand your solution. You can see I have data for one month for every 6 hour and I want the mean for the month (over all hours in the month - in this example December).

2 Kommentare

jonas
jonas am 27 Okt. 2018
If you load the data one month at a time, then you can just average all the data along the time dimension. If you load more than one month, then you need to split the data in n segments when taking the mean (where n is the number of months).
Jonas Damsbo
Jonas Damsbo am 27 Okt. 2018
Great! Thank you!

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Interpolation of 2-D Selections in 3-D Grids 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!

Translated by