Resize and sum a matrix
22 views (last 30 days)
I wasn't sure how to phrase my question, so apologies if it has been asked elsewhere and I couldn't find it!
What I want to do is re-size a Matrix, summing the values within adjacent elements together as it is re-sized. To give an example, let's say I start with a 4x4 Matrix:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
Then I want to re-size this into a 2x2 Matrix, summing adjacent values together such that the result is:
Where 1 + 2 + 5 + 6 = 14, ...11 + 12 + 15 + 16 = 54 etc.
The above is just an example, the numbers would normally be random. I would always be wanting to reduce the Matrix dimensions by an integer number, e.g. 500x500 to 100x100, 40x40 to 20x20 etc.
Is there a built in function, or a simple way to do this?
Thanks in advance
Adam on 11 Jan 2021
Edited: Adam on 11 Jan 2021
If you have the Image Processing Toolbox you can use blockproc something like the following:
array2 = blockproc( array1, [n n], @(b) sum( b.data(:) ) )
where n is the size of block that you want to sum together (2 in this case or e.g. 5 in the case of 500x500 to 100x100).
This assumes, as you stated, that it is always an integer division of the dimensions.