Annually averaging of a 4D matrix

7 Ansichten (letzte 30 Tage)
HA
HA am 12 Okt. 2020
Kommentiert: HA am 14 Okt. 2020
Hello,
I have a 4D data set (e.g. lon : lat : height : month) that I need to annually average. Usually with a 3D data set (e.g. lon : lat : month) I would use-
s=size(A);
B = squeeze(nanmean(reshape(A,[s(1:2),12,s(3)/12]),3));
For a 4D data set I have tried-
s=size(A);
B = squeeze(nanmean(reshape(A,[s(1:2:3),12,s(4)/12]),4));
I am guessing that I need to put something else in the square bracket or I am changing something wrong, but after trying various alterations I have decdied to ask the community.
Thank you.

Akzeptierte Antwort

Rik
Rik am 12 Okt. 2020
You are using the color operator incorrectly. 1:2:3 doesn't mean [1 2 3], but [1 3].
If your syntax does indeed work for you, the edit below should be what you're looking for.
s=size(A);
B = squeeze(nanmean(reshape(A,[s(1:3),12,s(4)/12]),4));
  1 Kommentar
HA
HA am 14 Okt. 2020
Aha, of course it does! Thank you, I can see how that is a silly mistake now!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by