Here's an outline to do what I think you're trying to do:
Step 1:
Find all of the matrix positions that have non-zero elements in their 3x5 neighborhood by summing the values of the neighbors. We don't want to include the current element just yet, hence the zero in the middle. Input matrix istest
.
fil = [1 1 1 1 1; 1 1 0 1 1; 1 1 1 1 1];
A = conv2(test, fil, 'same');
Step 2:
A
now contains values > 1, so change them all to 1, leaving the zero values alone.
A(A > 0) = 1;
Step 3:
Now that you've identified all of elements with valid neighborhoods, combinetest
with A
by element-wise multiplication leaving only those elements that are both non-zero in test
and have non-zero neighbors.
result = test .* A;
Step 0:
Padding - The above should work as is, but pads both rows and columns with zeros. As I understand them, your padding requirements are that you want circular rows and the columns padded with zeros.nCols = size(test, 2);
paddedTest = [zeros(1,nCols);...
test(:, nCols-1:nCols), test, test(:,1:2);...
zeros(1,nCols)];
This obviously changes the size of the matrix you're working on by doing your own padding, so you need to change the convolution parameters in order to return a matrix the same size as test
.
A = conv2(paddedTest, fil, 'valid');
I don't have Matlab available right now to test this, but hopefully others will correct any egregious errors I've made.