Sum specific dimension in matrix

1 view (last 30 days)
주희 박
주희 박 on 22 Dec 2022
Answered: Karim on 22 Dec 2022
Hi, I attached a matrix that consist of 7X9X240.
The 3rd dimension is time(20years X 12Month=240).
So I want to pick January ~March in every year and Sum all of them.
-> Matrix(:,:,1),(:,:,2),(:,:,3),(:,:,13),(:,:,14),(:,:,15)~
I mean, Sum each rows and colums.
So I hope 7X9 matrix ultimately.
How can I get the result?
In advence, thank you.

Accepted Answer

Karim
Karim on 22 Dec 2022
Hello, in the demo code below you can find some comments and a procedure on how to get the indexes, extract the data and sum over the third dimension.
load("Matrix.mat")
% create the indexes
Jan_Idx = 1 : 12 : 20*12; % january
Mar_Idx = 3 : 12 : 20*12; % march
% create the indexes for january and march
Idx = sort( [Jan_Idx Mar_Idx] )
Idx = 1×40
1 3 13 15 25 27 37 39 49 51 61 63 73 75 85 87 97 99 109 111 121 123 133 135 145 147 157 159 169 171
% extract these monts from the data
MyData = TDDb( :,:,Idx );
% now sum over the third dimension
MyData = sum( MyData, 3)
MyData = 7×9
8840 9116 9498 9385 9296 9206 9020 8772 7989 7615 7770 8399 8397 8368 8360 8694 8368 7866 6125 6607 7288 7688 7890 8004 8215 7844 7239 5025 5804 6759 7530 7944 8097 7915 7533 6775 3799 4547 5299 6436 7459 7761 7699 7460 6894 2723 2961 3711 4627 5733 6493 7032 7083 6846 1841 2212 2584 3027 4229 5389 6183 6492 6270

More Answers (0)

Community Treasure Hunt

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

Start Hunting!