Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Count Neighbors using Vectorization

Subject: Count Neighbors using Vectorization

From: Asif

Date: 15 Apr, 2009 08:13:01

Message: 1 of 5

Hi,

Say that you have a matrix:

A = [0 1 1; 0 1 0; 1, 1 0]
therefore, each element in the vector has the possibility of having 8 alive or dead neighbors, where alive = 1 and dead = 0.

Is there any way I can count the number of neighbors (dead/alive) of each element using vector addition/subtraction/multiplication/division. (NO LOOPS)

For example:
Given,
| 0 1 1 |
| 0 1 0 |
| 1 1 0 |

therefore, the first element (0) has 2 live neighbors and 1 dead neighbor. Similarly the second element in the first row has 2 live neighbors and 3 dead neighbors.

I dont want to use any loops.

any help would be highly appreciated

Subject: Count Neighbors using Vectorization

From: Gustavo Morales

Date: 23 Apr, 2009 18:51:01

Message: 2 of 5

"Asif ":
Hi..! Yesterday afternoon I saw your message. It seems interesting to me and this morning I wrote this:

function [m_alives m_deads] = alives(m)
%
[rows columns] = size(m);
m_a = zeros(rows+2,columns+2);
m_a(2:rows+1,2:columns+1) = m; %m ampliated with zeros all around
%%%%%% THE TRICK %%%%% (see <help circshift>)
m_alives = circshift(m_a,[ 0, 1])+... % left neighbors alives
                circshift(m_a,[-1, 1])+... % left&lower neighbors alives
                circshift(m_a,[-1, 0])+... % lower neighbors alives
                circshift(m_a,[-1,-1])+... % right&lower neighbors alives
                circshift(m_a,[ 0,-1])+... % right neighbors alives
                circshift(m_a,[ 1,-1])+... % right&upper neighbors alives
                circshift(m_a,[ 1, 0])+... % upper neighbors alives
                circshift(m_a,[ 1, 1]); % left&upper neigbors alives
%%%%%%%%%%%%%%%%%%
m_alives = m_alives(2:rows+1,2:columns+1); % truncating to the interest region
%
% m_neighbors: matrix with total neighbors.
% It should be a better (shorter) form to do it:
% Inner members have 8 neighbors:
m_neighbors = 8.*ones(rows,columns);
% Border members have 5 neighbors:
m_neighbors(1,:) = 5; m_neighbors(rows,:) = 5;
m_neighbors(:,1) = 5; m_neighbors(:,columns) = 5;
%Corners members have 3 neighbors
m_neighbors(1,1) = 3;
m_neighbors(1,columns) = 3;
m_neighbors(rows,1) = 3;
m_neighbors(rows,columns) = 3;
%And finally:
 m_deads = m_neighbors - m_alives;

I hope you like it...! Now, you could explain to me, what do you want this for?

Subject: Count Neighbors using Vectorization

From: Gustavo Morales

Date: 26 Apr, 2009 04:25:03

Message: 3 of 5

It should be a shorter version... Maybe Bruno Luong or Roger Stafford or John D'Errico can do it in 3 lines of code!!!!!

Subject: Count Neighbors using Vectorization

From: Bruno Luong

Date: 26 Apr, 2009 07:24:02

Message: 4 of 5

"Gustavo Morales" <gustavo.morales.2000@gmail.com> wrote in message <gt0nmv$eb1$1@fred.mathworks.com>...
> It should be a shorter version...

Indeed Gustavo, a versatile tool where people on image processing use almost every day is convolution:

% Data
A=double(rand(6)>0.4)
% Kernel
B=ones(3); B(2,2)=0; % Count neighbors, not self
% Engine
live=conv2(A,B,'same')
dead=conv2(1-A,B,'same')

% Bruno

Subject: Count Neighbors using Vectorization

From: Gustavo Morales

Date: 26 Apr, 2009 07:46:01

Message: 5 of 5

Bruno Luong:

Thanks...! I've learnt

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us