Got Questions? Get Answers.
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:
Neighbor Indexing

Subject: Neighbor Indexing

From: Sarah Jones

Date: 26 Nov, 2010 23:00:06

Message: 1 of 6

I want to write a function that changes the state of a randomly chosen cell in an mxn matrix based on the state of its 8 neighbors and itself. Because the cell will be randomly chosen, I must account for neighboring cells beyond the edge boundaries by having values picked up by wrapping around to another row and/or column. How do I do this?

Subject: Neighbor Indexing

From: ImageAnalyst

Date: 26 Nov, 2010 23:20:04

Message: 2 of 6

Use rand() to pick the cell location - the row, column in the cell
array of the cell that you are going to change. Wait a minute...are
you talking about a cell array, or a numerical matrix? What is the
data type of the entity that you're talking about?

Anyway, just calculate the (row,column) of each of the 8 neighbor
cells (or elements depending on what you're using) and then if it's
greater than the max row or column, just subtract the width or height
to wrap it around to the beginning side (down around where the row or
column = 1). Or if the row,column is negative, just add the width or
height to wrap it around on the high end. Alternatively just pad your
cell array (or matrix) with a layer all the way around that copies the
other side.

Subject: Neighbor Indexing

From: Sarah Jones

Date: 27 Nov, 2010 00:24:04

Message: 3 of 6

Thank you for your prompt response, ImageAnalyst! I'm talking about an MxN grid of zeros and ones. If the majority of the 9 cells is 1, the updated cell becomes 0. If the majority of the 9 cells is 0, the updated cell becomes 1. My plan is to sum the states (0 or 1) of the 9 cells and if the sum is greater than or equal to 5, the majority is clearly 1 and the state will change to 0, etc. I understand how to create the random grid of zeros and ones and pick the random cell in the grid. What I am still confused about is how to incorporate the wrap around command within one loop. So far, I have accounted for the 9 cells when the chosen cell's neighbors are within the bounds:

sum(m, n) = [grid(m, n) + grid(m-1, n) + grid(m-1, n-1) + grid(m, n-1)+... +grid(m+1, n-1) + grid(m+1, n) + grid(m+1, n+1) + grid(m, n+1) + grid(m-1, n+1)]

When you say width and height is that the number of rows and number of columns in the grid?

Thank you again! So much!

Subject: Neighbor Indexing

From: Roger Stafford

Date: 27 Nov, 2010 01:56:03

Message: 4 of 6

"Sarah Jones" <enewton6@gmail.com> wrote in message <icpj34$242$1@fred.mathworks.com>...
> Thank you for your prompt response, ImageAnalyst! I'm talking about an MxN grid of zeros and ones. If the majority of the 9 cells is 1, the updated cell becomes 0. If the majority of the 9 cells is 0, the updated cell becomes 1. My plan is to sum the states (0 or 1) of the 9 cells and if the sum is greater than or equal to 5, the majority is clearly 1 and the state will change to 0, etc. I understand how to create the random grid of zeros and ones and pick the random cell in the grid. What I am still confused about is how to incorporate the wrap around command within one loop. So far, I have accounted for the 9 cells when the chosen cell's neighbors are within the bounds:
>
> sum(m, n) = [grid(m, n) + grid(m-1, n) + grid(m-1, n-1) + grid(m, n-1)+... +grid(m+1, n-1) + grid(m+1, n) + grid(m+1, n+1) + grid(m, n+1) + grid(m-1, n+1)]
>
> When you say width and height is that the number of rows and number of columns in the grid?
>
> Thank you again! So much!
- - - - - - - - -
  If the randomly selected element is at grid(i,j) with 'grid' being an m x n array, do this to update it:

  grid(i,j) = +(sum(sum(grid(mod(i-2:i,m)+1,mod(j-2:j,n)+1)))<5);

(The initial "+" is to convert the logical variable back to numerical if needed.)

Roger Stafford

Subject: Neighbor Indexing

From: ImageAnalyst

Date: 27 Nov, 2010 02:28:16

Message: 5 of 6

Roger's solution looks good. If you want to do it for *every single
element* in the matrix, I suggest you look at the conv2() function.
First pad the array with wrapped around values, and then do something
like

% Compute the sum in every single 3x3 window.
convolvedImage = conv2(paddedImage, ones(3), 'same');
% Initialize our output variable.
outputImage = zeros(size(convolvedImage));
% Flip those with sum less than 5
outputImage(convolvedImage < 5) = 1;

Finally, look up cell in the help. To MATLAB, "cell" means something
different that "element." Even though you're looking at the array of
numbers in the variable editor, those are not "cells" even though
Excel calls them that. Cells has a completely different meaning in
MATLAB. To MATLAB, a cell is like a bucket - a bucket that you can
throw *any* type of variable into. And a "cell array" is an array of
cells where the cells do not have to have the same kind of variable in
each bucket. For example you could have a cell array of 1 by 4 cells
where the first cell contains an integer, the second cell contains a
3D floating point matrix, the third cell contains a character array
(string), and the fourth cell contains a structure. Think of a cell
array as an array of buckets on your floor where you can toss
*anything* into. Each bucket is one cell.
ImageAnalyst

Subject: Neighbor Indexing

From: JANNIEFERRELL

Date: 25 Dec, 2010 17:43:10

Message: 6 of 6

The <a href="http://bestfinance-blog.com">loans</a> are useful for guys, which want to start their organization. As a fact, that is not very hard to receive a term loan.

Tags for this Thread

No tags are associated with 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