473,387 Members | 1,582 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Array neighbourhoods

Hi again.

One thing I forgot to include in my post the other day. I was wondering if
anyone knew of any fast algorithms for returning the surrounding neighbours
of an array element, or even the neighbours of a group of array elements.

I'm using a function which affects neighbourhoods of a value with a
diminishing effect (i.e. a gaussian distribution), so I need to be able to
get the neighbourhood (of size 1) of the first array entry, as well as the
neighbourhood of the returned neighbourhoods! Hope that all makes sense.

Daniel

__________________________________________________ _______________
Express yourself with cool new emoticons http://www.msn.co.uk/specials/myemo
Jul 18 '05 #1
1 1571
"Daniel Pryde" <ab********@hotmail.com> wrote in message
news:ma************************************@python .org...
Hi again.

One thing I forgot to include in my post the other day. I was wondering if
anyone knew of any fast algorithms for returning the surrounding neighbours of an array element, or even the neighbours of a group of array elements.

Try this (requires Python 2.3, since it uses sets). Can't say how fast it
is, but it handles all cases I can think of (edge effects, disjoint cells,
etc.). Maybe start with this to get going, then make it faster if it needs
to be.
# matrixNeighbors.py
from sets import Set

def neighborhoodOf(cells, rows, cols):
ret = Set()
if type(cells) == tuple:
row,col = cells
for i in (-1,0,1):
for j in (-1,0,1):
ret.add( (row+i, col+j) )
# remove original cell
ret.remove( cells )

elif type(cells) == list:
for cell in cells:
for neighbor in neighborhoodOf(cell,rows,cols):
ret.add(neighbor)
# remove original cells
ret -= Set( cells )

# remove all entries in ret with negative values,
# or values greater than number of cols/rows
for x,y in ret.copy():
if x < 0 or y < 0 or x >= COLS or y >= ROWS:
ret.remove( (x,y) )

return list(ret)

# define matrix boundaries
ROWS = 4
COLS = 6

print neighborhoodOf( (0,0), ROWS, COLS )
print neighborhoodOf( (COLS-1,ROWS-1), ROWS, COLS )
print neighborhoodOf( (COLS,ROWS), ROWS, COLS )
print neighborhoodOf( [(0,0),(0,1)], ROWS, COLS )
print neighborhoodOf( neighborhoodOf( (1,1), ROWS, COLS), ROWS, COLS )
print neighborhoodOf( neighborhoodOf( (COLS-1,ROWS-1), ROWS, COLS), ROWS,
COLS )
print neighborhoodOf( neighborhoodOf( (COLS,ROWS), ROWS, COLS), ROWS, COLS )
Jul 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Brian | last post by:
I'm diddlying with a script, and found some behavior I don't understand. Take this snippet: for ($i = 0; $i <= count($m); $i++) { array_shift($m); reset($m); }
2
by: Stormkid | last post by:
Hi Group I'm trying to figure out a way that I can take two (two dimensional) arrays and avShed and shed, and subtract the matching elements in shed from avShed I've pasted the arrays blow from a...
15
by: lawrence | last post by:
I wanted to test xml_parse_into_struct() so I took the example off of www.php.net and put this code up on a site: <?php $simple = <<<END <item>
8
by: vcardillo | last post by:
Hello all, Okay, I am having some troubles. What I am doing here is dealing with an employee hierarchy that is stored in an array. It looks like this: $employees = array( "user_id" => array(...
12
by: Sam Collett | last post by:
How do I remove an item with a specified value from an array? i.e. array values 1,2,2,5,7,12,15,21 remove 2 from array would return 1,5,7,12,15,21 (12 and 21 are NOT removed, duplicates are...
8
by: Mike S. Nowostawsky | last post by:
I tried using the "toUpperCase()" property to change the value of an array entity to uppercase BUT it tells me that the property is invalid. It seems that an array is not considered an object when...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
35
by: VK | last post by:
Whatever you wanted to know about it but always were affraid to ask. <http://www.geocities.com/schools_ring/ArrayAndHash.html>
11
by: deko | last post by:
I need to create a basic one-dimensional array of strings, but I don't know how many strings I'm going to have until the code is finished looping. pseudo code: Dim astrMyArray() Do While Not...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.