Monday, July 27, 2009

Matlab: counting zeros neighbouring each element

Q:

I have matrix x, I want to count for each element the number of zeros in the neighbouring 8 element.

For example, if

x = [
1 0 0
0 1 1
0 1 0];

I want to obtain count

c = [
2 2 1
2 5 3
1 3 0];

A:
c = conv2([1 1 1; 1 0 1; 1 1 1],double(~x),'same');


OR


c = conv2([1 1 1],[1 1 1],double(~x),'same')-~x;

No comments: