Path: news.mathworks.com!not-for-mail
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$1@fred.mathworks.com>
References: <icpe5m$llt$1@fred.mathworks.com> <ccd6261b-2090-447d-af5a-e90f2a26af77@o4g2000yqd.googlegroups.com> <icpj34$242$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-03-blr.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1290822963 7292 172.30.248.38 (27 Nov 2010 01:56:03 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Sat, 27 Nov 2010 01:56:03 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1187260
Xref: news.mathworks.com comp.soft-sys.matlab:690364

"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