From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Neighbor Indexing
Date: Sat, 27 Nov 2010 01:56:03 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 16
Message-ID: <icpofj$73s$>
References: <icpe5m$llt$> <> <icpj34$242$>
Reply-To: <HIDDEN>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: 1290822963 7292 (27 Nov 2010 01:56:03 GMT)
NNTP-Posting-Date: Sat, 27 Nov 2010 01:56:03 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1187260
Xref: comp.soft-sys.matlab:690364

"Sarah Jones" <> wrote in message <icpj34$242$>...
> 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