# Resize and sum a matrix

19 views (last 30 days)
Jonathan on 11 Jan 2021
Commented: Jonathan on 12 Jan 2021
Hi,
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:
array1 =
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:
array2 =
14 22
46 54
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?

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.
Jonathan on 12 Jan 2021
Thanks, that gives me exactly the result I was looking for.

Walter Roberson on 11 Jan 2021
N = 3;
array1 = randi(9, 12, 9)
array1 = 12×9
5 7 1 7 2 6 3 1 9 4 6 9 2 1 5 5 9 1 7 7 2 1 6 1 4 9 9 4 9 5 4 5 1 7 9 7 2 6 5 3 7 9 3 6 8 7 7 9 5 4 1 8 8 4 6 1 9 1 2 7 8 1 3 1 9 5 5 2 2 3 1 7 2 1 5 3 9 9 3 4 9 3 8 7 3 6 1 7 9 5
S = conv2(array1, ones(N,N), 'same')
S = 12×9
22 32 32 22 23 22 29 28 20 36 48 42 31 31 33 43 50 38 37 53 45 35 26 35 50 60 44 35 47 42 38 37 43 49 62 48 35 54 53 47 39 45 52 60 42 29 52 46 45 39 49 51 49 30 31 54 51 42 29 37 39 43 24 20 39 39 41 40 45 38 39 25 24 41 46 45 40 42 39 48 35 21 37 37 42 39 47 45 47 32
d = floor((N+1)/2)
d = 2
S(d:N:end,d:N:end)
ans = 4×3
48 31 50 54 39 60 39 40 39 43 34 42
N2 = 4;
array2 = randi(9, 12, 16)
array2 = 12×16
2 9 8 3 7 5 7 1 2 7 7 2 9 3 9 1 7 1 7 9 3 8 6 4 8 1 9 1 1 2 3 9 2 7 9 3 9 3 3 5 5 5 5 7 2 9 7 5 1 4 6 3 6 7 5 8 1 2 8 5 3 3 4 4 8 8 1 6 7 1 4 2 9 7 7 6 1 4 5 5 7 9 2 5 5 4 3 8 5 2 6 5 2 1 2 6 7 2 9 9 6 4 6 3 4 8 6 3 9 9 1 5 1 6 7 9 7 1 4 2 6 6 9 1 1 3 4 2 3 8 5 8 4 4 8 1 7 7 2 1 1 4 6 9 4 1 3 2 2 7 9 3 5 6 4 6 4 9 1 6
S2 = conv2(array2, ones(N2,N2), 'same')
S2 = 12×16
52 67 75 74 66 61 57 54 59 59 56 57 55 60 48 34 63 81 94 96 87 87 78 70 78 75 74 76 70 74 59 42 61 82 89 88 83 81 79 75 86 86 70 73 63 67 60 42 64 81 90 77 74 80 73 74 85 85 73 74 66 63 55 38 64 87 88 81 81 79 74 77 86 84 80 78 63 64 49 32 67 96 98 83 81 67 66 79 90 90 79 73 57 60 47 30 66 97 101 89 87 70 70 80 82 78 69 63 53 65 52 35 56 84 88 87 90 71 74 85 79 81 74 72 63 74 59 34 51 72 74 69 78 70 76 83 74 78 71 75 73 76 63 38 45 62 59 58 68 64 77 86 73 84 80 87 90 86 68 38
d2 = floor((N2+1)/2)
d2 = 2
S2(d2:N2:end,d2:N2:end)
ans = 3×4
81 87 75 74 96 67 90 60 62 64 84 86

Bruno Luong on 11 Jan 2021
Edited: Bruno Luong on 11 Jan 2021
If you isist on "resize"
array1 =[1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16];
[m,n] = size(array1);
array2 = sum(permute(reshape(array1,[2 m/2 2 n/2]),[2 4 1 3]),[3 4])

### Categories

Find more on Matrix Indexing in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!