Hello,
It has been a while since I have made any new loops that I have not copied from old scripts, so I am a little out of practice. This question should be fairly easy for most to answer.
I have a data set with monthly data:
Size(A)=192,144,120
I would like to put this into ten, yearly averages:
Size(B)=192,144,10
I have just done it the long way around-
B(:,:,1)=mean(A(:,:,1:12),3);
B(:,:,2)=mean(A(:,:,13:24),3);
B(:,:,3)=mean(A(:,:,25:36),3);
B(:,:,4)=mean(A(:,:,37:48),3);
B(:,:,5)=mean(A(:,:,49:60),3);
B(:,:,6)=mean(A(:,:,61:72),3);
B(:,:,7)=mean(A(:,:,73:84),3);
B(:,:,8)=mean(A(:,:,85:96),3);
B(:,:,9)=mean(A(:,:,97:108),3);
B(:,:,10)=mean(A(:,:,109:120),3);
This works fine, but is a little tedious and I will be wanting to do this with much larger data sets in the coming months.
What is the best way to do this in a for loop?
Thank you!

댓글 수: 2

Is there a specific reason for a loop? I think reshape should work aswell:
B=mean(reshape(A,192,144,12,[]),4);
HA
HA 2018년 11월 29일
This does not quite give me what I want.

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

 채택된 답변

Andrei Bobrov
Andrei Bobrov 2018년 11월 29일

2 개 추천

s = size(A);
B = squeeze(mean(reshape(A,[s(1:2),12,s(3)/12]),3));

댓글 수: 2

HA
HA 2018년 11월 29일
This Works well, thank you!
Marek Harenda
Marek Harenda 2022년 4월 1일
Hello sir, can you help me with changing this code into calulation of the monthly avarage by which i mean calculating January, February, etc. mean from the whole dataset? I am asking you, because your code for annual mean is working with my data so I hope that I only need to do some little change to achieve my goal. I know that there are topics more related to my problem, but the proposals for solving it turned out not to be satisfactory for me. I see that in this case we need only to extract 12 consecutive months for each year, but in my case I probably need to extract every 12 month with 1 month shift so it is a bit more complicated for me.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

HA
2018년 11월 29일

댓글:

2022년 4월 1일

Community Treasure Hunt

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

Start Hunting!

Translated by