필터 지우기
필터 지우기

Finding the sum of array

조회 수: 2 (최근 30일)
Vaultec
Vaultec 2015년 1월 15일
댓글: Timmy 2015년 1월 19일
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

채택된 답변

Timmy
Timmy 2015년 1월 15일
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
Vaultec
Vaultec 2015년 1월 16일
편집: Vaultec 2015년 1월 16일
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 2015년 1월 19일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Image Analyst
Image Analyst 2015년 1월 15일
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 2015년 1월 16일
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

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by