How do I sum over one dimension of a multidimensional array?
이전 댓글 표시
I have four dimensions in a netcdf file: lat, long, sector and time. I want to sum over JUST sector. Is there a simple command to do this?
채택된 답변
추가 답변 (1개)
Juan Fernandez
2018년 6월 21일
0 개 추천
Yes, there is a simple single command. Use the sum(A,dim) function. If this is not working, then provide additional information and perhaps I can be of more assistance.
댓글 수: 5
Nesha Wright
2018년 6월 23일
Walter Roberson
2018년 6월 23일
permute(dimsum, [1 2 4 3])
or
squeeze(dimsum)
Louis-Philippe Guinard
2021년 3월 24일
Tracing back to Juan's answer, if I wanted to sum, say, just the N first elements of the 3rd dimension, how would one go about that?
I have a very similar problem, as in, a 4D array. I treat it like a 256x256 image at 256 different timestamps, for 30 acquisitions. In this regard, my dimensions would be x, y, t and acq. I want to add the 15 first t's while leaving all other dimensions intact, I don't care what happens to the rest of the t's. How would that be possible?
Thanks in advance!
Adam Danz
2021년 3월 24일
> if I wanted to sum, say, just the N first elements of the 3rd dimension, how would one go about that?
sum(x(:,:,1:N), 3)
Louis-Philippe Guinard
2021년 3월 25일
I wasn't too far off, I did sum(x(:,:,1:N)) and returned a 1x256xN array. Aight thanks a lot! :)
카테고리
도움말 센터 및 File Exchange에서 Data Import and Analysis에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!