How do I calculate monthly mean of a 3-D matrix?

조회 수: 15 (최근 30일)
Levente Samu
Levente Samu 2020년 9월 12일
댓글: Ronald 2024년 4월 8일 11:48
Hello all,
I have a 111x151x14243 matrix (lat, lon, time), called cmprecip, containing daily total precipitation data and I have a 14243x1 datetime array, called cmtime, in 'dd.MM:yyyy' format. I would like to calculate monthly mean from the daily data. So the output would be a 111x151x468 matrix. As a rookie in Matlab, my initial thought was to concatenate the matrix and the array and then use 'retime' just as I did before with 2-D arrays, but I was not able to make it work.
Is there a way to solve this issue? Thank you guys for your help in advance!
Levente

채택된 답변

jonas
jonas 2020년 9월 12일
Using retime is one option, you would just have to shape your array into a timetable first, which is a bit of a hassle.
I think this should work as well
%some data
A = 1:10000;
A = reshape(A,10,10,100);
t = (linspace(datetime(2000,1,1),datetime(2005,1,1),100))';
%find unique month ids
[U,~,G] = unique([year(t),month(t)],'rows');
%preallocate output variable
out = nan(size(A,1),size(A,2),size(U,1));
%loop over months
for i = 1:size(U,1)
f = A(:,:,G==i);
out(:,:,i) = mean(f,3);
end
  댓글 수: 3
jonas
jonas 2020년 9월 13일
My pleasure!
Ronald
Ronald 2024년 4월 8일 11:48
Thanks Jonas for your answer. It solves my querry for 3D timeseries temporal reshaping. I will link it as an answer to my question too.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by