Finding the sum of array

5 views (last 30 days)
Vaultec
Vaultec on 15 Jan 2015
Commented: Timmy on 19 Jan 2015
What I'm trying to do is to find the sum of the surrounding elements of an binary array
For example
[1 0 0 1;
1 0 1 0;
0 0 1 1;
1 0 0 0]
would return
[1 3 2 1;
1 4 3 3;
2 4 2 2;
0 2 2 2]
but is there a way to do so without using conv2? as I dont really understand what conv2 does

Accepted Answer

Timmy
Timmy on 15 Jan 2015
You can padded the array with 0's on all side. Then, run two for-loop to compare A and B.
A = randi([0 1], 100, 100);
B = [1 1 1;1 0 1;1 1 1];
A_prime = zeros(102);
A_prime(2:101,2:101) = A;
Sum = zeros(100);
for i = 2:101
for j = 2:101
Sum(i,j) = sum(sum(A_prime(i-1:i+1,j-1:j+1) == B));
end
end
  2 Comments
Vaultec
Vaultec on 16 Jan 2015
Edited: Vaultec on 16 Jan 2015
when I run it and check it with Conv2, the numbers dont match up. It seems that the answer provided by the two for-loop has some values that are 1 more than the correct value attained from running conv2
Also could you explain why sum(A_new(m-1:m+1,n-1:n+1) == B) returns a 3 vector?
Timmy
Timmy on 19 Jan 2015
If you make B = [1 1 1; 1 -1 1; 1 1 1], -1 can be other number but 0 and 1, then you will get the correct value.
This part "A_prime(i-1:i+1,j-1:j+1) == B" will match a 3x3 matrix from the main matrix A_prime to the window B. That why you got the extra count if the middle value is 0. If they are equal, it will be 1. And if not, then it will be 0. The inner "sum" sums up the column, I believe. The outer "sum" sums up the row.

Sign in to comment.

More Answers (2)

Image Analyst
Image Analyst on 15 Jan 2015
How about you just understand what conv2() does in that case. Basically it slides a window along and multiplies the window values (all 1's in the case where you want to do a sum) by the values of the larger matrix and then sums the products. So if they're all 1 except the middle is 0, it just multiplies the larger matrix by one and sums. So the value of the output at the pixel location is simply the sum of the original image matrix values, except for the center pixel itself, in a window centered around that pixel.

Image Analyst
Image Analyst on 16 Jan 2015
Timmy's code doesn't work because it's messed up and not robust. Here is a fixed version of his code that does work and is more robust (though it could be more robust/general because it assumes B must be a 3 by 3 matrix):
A = [1 0 0 1;
1 0 1 0;
0 0 1 1;
1 0 0 0]
B = [1 1 1;1 0 1;1 1 1]; % Code assumes this is a 3x3 matrix.
[rows, columns] = size(A);
A_prime = zeros(rows+2, columns+2);
A_prime(2:(1+rows),2:(1+columns)) = A;
theCounts = zeros(size(A_prime));
for col = 2 : size(A_prime, 2)-1
for row = 2 : size(A_prime, 1)-1
thisWindow = (A_prime(row-1:row+1,col-1:col+1) .* B) ~= 0;
theCounts(row,col) = sum(thisWindow(:));
end
end
theCounts = theCounts(2:end-1, 2:end-1)
In the command window
A =
1 0 0 1
1 0 1 0
0 0 1 1
1 0 0 0
theCounts =
1 3 2 1
1 4 3 4
2 4 2 2
0 2 2 2

Categories

Find more on Multidimensional Arrays 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!