how to do average over third dimension?

Hi all,
I have multiple 3D matrices with the dimension of 21*41*24 for each which 24 (third dimension) is 24 hours.
I wanted to calculate 6 hours average for the third dimension.
Where I am mistaken?
NHours=size(ucom,3);
NDays=NHours/6;
out = nanmean(permute(reshape(ucom,[6,NDays]),[2,1,3]),3);

댓글 수: 2

KSSV
KSSV 2021년 7월 26일
Read about mean there is option to get mean in any dimension.
Lilya
Lilya 2021년 7월 26일
OK..Thank you :)

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

 채택된 답변

Star Strider
Star Strider 2021년 7월 26일

0 개 추천

Taking a slightly shorter example, try something like this —
% ucom = randi(9,21,41,24);
ucom = randi(9, 3, 4, 24);
sz_ucom = size(ucom);
ucomr = reshape(ucom,sz_ucom(1),sz_ucom(2),[],6);
out = nanmean(ucomr,4)
out =
out(:,:,1) = 5.6667 5.5000 4.8333 3.1667 5.8333 4.5000 5.6667 4.0000 5.8333 5.1667 5.0000 4.5000 out(:,:,2) = 3.8333 6.0000 4.5000 6.0000 6.0000 5.1667 5.6667 2.5000 3.1667 6.0000 5.3333 5.3333 out(:,:,3) = 3.8333 3.6667 6.3333 5.3333 4.1667 6.0000 5.5000 5.6667 3.6667 5.0000 4.1667 6.3333 out(:,:,4) = 5.5000 5.5000 4.1667 2.6667 3.1667 3.6667 4.8333 4.8333 7.0000 3.8333 5.5000 6.3333
The ‘out’ variable now has 4 blocks of 6-hour averages
.

댓글 수: 8

Lilya
Lilya 2021년 7월 26일
Much appreciated Sir Strider :)
It works perfectly!
Star Strider
Star Strider 2021년 7월 26일
As always, my pleasure!
.
Lilya
Lilya 2021년 7월 26일
Can i know to to add multiple 3d to new 3d one?
ex: the 'out' matrix should be 365 for each day in a year. The size of each 'out' is (21*41*4).
I want to have a final matrix contains all 'out' to get (21*41*1460), where 1460 is 4*365
I tried 'cat' function but it doesn't take all 'out', just only the last one.
Thanks in advance.
I am not certain that I understand what you want to do, or the matrices you want to .
It might be easier to concatenate the ‘ucom’ matrices in the third dimension, the do the resshape operation and calcuiate the mean.
Example —
% ucom = randi(9,21,41,24);
ucom1 = randi(9, 3, 4, 24);
ucom2 = randi([11 19], 3, 4, 24);
ucom12 = cat(3, ucom1, ucom2);
sz_ucom = size(ucom12)
sz_ucom = 1×3
3 4 48
ucom12r = reshape(ucom12,sz_ucom(1),sz_ucom(2),[],6);
sz_ucom12r = size(ucom12r)
sz_ucom12r = 1×4
3 4 8 6
out = nanmean(ucom12r,4)
out =
out(:,:,1) = 9.5000 12.1667 10.1667 10.6667 9.3333 10.1667 11.3333 9.3333 10.5000 9.5000 9.1667 10.5000 out(:,:,2) = 9.6667 10.3333 9.3333 10.3333 10.0000 9.5000 9.1667 8.6667 10.1667 8.8333 10.8333 11.6667 out(:,:,3) = 9.8333 10.3333 9.0000 12.5000 9.6667 10.6667 10.3333 8.8333 10.3333 9.0000 12.3333 9.8333 out(:,:,4) = 10.0000 8.6667 10.6667 11.6667 9.8333 8.8333 9.3333 9.3333 10.8333 11.1667 10.1667 10.5000 out(:,:,5) = 10.3333 9.6667 9.3333 10.3333 11.6667 10.1667 10.3333 8.6667 10.3333 9.8333 9.1667 8.8333 out(:,:,6) = 11.1667 11.0000 9.3333 10.6667 8.1667 11.0000 9.0000 10.0000 11.5000 8.1667 9.5000 11.5000 out(:,:,7) = 10.5000 11.1667 8.5000 8.3333 10.8333 11.3333 7.6667 12.0000 9.5000 10.6667 10.8333 10.3333 out(:,:,8) = 10.3333 10.8333 8.8333 11.1667 11.1667 8.6667 10.8333 9.0000 9.5000 10.8333 10.8333 9.5000
size(out)
ans = 1×3
3 4 8
It would likely be necessary to do the concatenation in a loop with 365 matrices.
That is the only way I can think of to approach this.
.
Exactly!
Thank you very, very much for your response.
result = [];
nfiles = dir('*.nc');
for i=1:length(nfiles)
for j = 1:1460
fname = nfiles(i).name;
ucom = ncread(fname,'U10M');
sz_ucom = size(ucom);
ucomr = reshape(ucom,sz_ucom(1),sz_ucom(2),[],6);
out = nanmean(ucomr,4);
result(:,:,j) = out;
end
end
I've used that but this error appears
Unable to perform assignment because the indices on the left side are not compatible with the size of the right side.
As always, my pleasure!
In each iteration, ‘out’ contains multiples of 4 elements in the 3rd dimension, so a single index reference is not going to work.
Rather than using an indexing scheme (that may not work well in all situations), I would just save ‘out’ to a cell array —
result{j} = out;
Recover the matrices from it later.
.
Lilya
Lilya 2021년 7월 26일
It works!!
Much much appreciated.
Star Strider
Star Strider 2021년 7월 26일
As always, my pleasure!
(Sorry for the delay — I was off doing other things.)
.

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

추가 답변 (0개)

카테고리

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

태그

질문:

2021년 7월 26일

댓글:

2021년 7월 26일

Community Treasure Hunt

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

Start Hunting!

Translated by