问题
I am interested in calculating GPH anomalies in Matlab. I have a 3D matrix of lat, lon, and time. Where time is a daily GPH value for 32 years (1979-2010). The matrix is 95x38x11689. How do I compute a daily average across all years for each day of data, when the matrix is 3D?
I am ultimately trying to compute the GPH anomaly from the difference of the daily value and the climatological mean of that given day.
In other words, how do I compute the average of Jan. 1st dates for all years to compute the climatological mean of all Jan. 1st's from 1979-2010? And so forth for each day after. The data also includes leap years. How do I handle that?
Thanks!
UPDATE: The 3rd dimension is the GPH data NOT time. It's just the data for every day time step from 1979 to 2011.
回答1:
Try something like this:
%Using your dimensions make up some fake data
GPH = rand(95,38,11689)
%Get an index into the 3rd dimension for your desired dates
dateIdx = datenum(1979:2010,1,1)-datenum(1979,1,1) + 1;
%Take the mean along the 3rd dimensions
mean_of_all_Jan_1 = mean(GPH(:,:,dateIdx ),3);
An example of getting another arbitrary date say every March 15th. Note that I only changed the first call to datenum
and not the second.
dateIdx = datenum(1979:2010,3,15)-datenum(1979,1,1) + 1;
mean_of_all_March_15 = mean(GPH(:,:,dateIdx ),3);
I am assuming that along the 3rd dimension index 1 is Jan 1 1979 & progresses at 1 index per day.
EDIT to get every day
This will return a dailyMeanGPH
that is 95x38x366
. Such that dailyMeanGPH(:,:,1) is the mean for every Jan 1st, dailyMeanGPH(:,:,2) = Jan 2nd ...
Note that for Feb 29th there will be less days averaged since it will occur 1/4th as often.
Alternative you if comment/uncomment the lines noted in the code below your output can be returned in a cell array. That way if you wanted Jan 1 mean you would do dailyMeanGPH{1,1}
then Jan 2nd dailyMeanGPH{1,2}
etc. Where the index into the cell is {month,day}
of interest.
function dailyMeanGPH = getDailyMeanGPH(GPH)
daysInMonth = [31 29 31 30 31 30 31 31 30 31 30 31]; %inlcude 29 for Feb
dailyMeanGPH = zeros(size(GPH,1),size(GPH,2),sum(daysInMonth)); %COMMENT OUT IF YOU WNAT OUTPUT IN CELL
% dailyMeanGPH = cell(12,31); %UNCOMMENT IF YOU WANT OUTPUT IN CELL
k = 1;
for m = 1:12
for d = 1:daysInMonth(m)
%Get an index into the 3rd dimension for your desired dates
dateIdx = datenum(1979:2010,m,d)-datenum(1979,1,1) + 1;
if m==2 && d ==29 %Purge Feb 29 from non-leap years.
checkIdx = datenum(1979:2010,3,1)-datenum(1979,1,1) + 1;
dateIdx = setdiff(dateIdx,checkIdx);
end
%Take the mean along the 3rd dimensions.
dailyMeanGPH(:,:,k) = mean(GPH(:,:,dateIdx ),3);%COMMENT OUT IF YOU WNAT OUTPUT IN CELL
k = k+1;
% dailyMeanGPH{m,d} = mean(GPH(:,:,dateIdx ),3);%UNCOMMENT IF YOU WANT OUTPUT IN CELL
end
end
来源:https://stackoverflow.com/questions/47065739/how-to-calculate-anomalies-of-geopotential-height-in-matlab