Loop how to sum part of matrix

1 view (last 30 days)
NINA BAJEC
NINA BAJEC on 19 Jan 2021
Edited: NINA BAJEC on 19 Jan 2021
if I have a 9x3 matrix A=[123;123;456;212;121;111;312;141;010] AND i want to get matrix B dimension 3x3 (see photo) so that cell 1,1 would have a sum of 1+2+3 (so in first coulmn of A matrix taking 1,1 cell and 4,1 cell and 7,1 cell) and in the entry of matrix B 2,1 it would be 1+1+1 (taking from matrix A cell 2,1 and 5,1 and 8,1) and in matrix B the entry 3,1 would take from A 4+1+0 so 3,1 cell and 6,1 cell and 9,1 cell and the same logic for column 2 of B and column 3 of B to get at the end B=[647;385;577]
This was just an example in reality I need the same logic for much larger matrix dimension (from 1435x35 getting it into 35x35) so I would be really grateful for loop command or some other help to get the logic of this....

Answers (1)

Cris LaPierre
Cris LaPierre on 19 Jan 2021
Reshape your matrix to a 3x3x3 and then sum along the 3rd dimension.
  2 Comments
NINA BAJEC
NINA BAJEC on 19 Jan 2021
Edited: NINA BAJEC on 19 Jan 2021
could you please help me with the reshape command and how then to sum every 3rd dimension in a row... I also need to get 3x3 B matrix and this summing over 3rd dimension would give me 3x1 outcome?
Cris LaPierre
Cris LaPierre on 19 Jan 2021
The documentation I linked to contains examples. Take a stab at it yourself first. If you get stuck, share your code and a specific question about what you are not understanding.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!