2

I've been working on a code (Py 2.7) that generates an array of elements with each node assigned some random numbers. Now, I wish to make a list of the surrounding elements, and find the index of the max value. The array size is variable (I considered col = array column size). I have assigned numbers to each node (I called it 's' in the below) so that I can find the 2D index of the array element. Here is what I wrote

rn = s/col; cn = s%col;
b = [rr[rn,cn+1],rr[rn-1,cn+1],rr[rn-1,cn],rr[rn-1,cn-1],rr[rn,cn-1],rr[rn+1,cn-1],rr[rn+1,cn],rr[rn+1,cn+1]]
ma = max(b)
a = [i for i,j in enumerate(b) if j == ma]

Is there any short method to find the neighbours without the need to number each array element ? (like I did using s).

AvidLearner
  • 4,123
  • 5
  • 35
  • 48
Bhanu Chander
  • 390
  • 1
  • 6
  • 16
  • Have you looked at http://docs.scipy.org/doc/numpy/reference/generated/numpy.array.html? – boardrider Jun 13 '15 at 12:57
  • your question is confusing. try to put it in a very simple words . with expected out or what u want to acheive . – coder3521 Jun 13 '15 at 13:05
  • @boardrider Yes, I refereed to that link – Bhanu Chander Jun 14 '15 at 06:50
  • @csharpcoder My code is intended to create a 2D array of varying row and column size, and assign rand no's (preferably taken from uniform distribution). The code should allow the user to input start and end node of the selected array. So, I made a numbered matrix of assumed row and column size (avoiding array index) which I called s (state). The program needs to start from the start state and move till end such that at each state, it moves to the next state which has max number (among the neighbours). – Bhanu Chander Jun 14 '15 at 06:58
  • @csharpcoder - the matrix of numbers that is created is visible to the user, whom can choose two numbers from the matrix form as start and end states. – Bhanu Chander Jun 14 '15 at 07:14

1 Answers1

3

You can use numpy for this. First, let's create a random 5x5 matrix M for testing...

>>> M = np.random.random((5, 5))
>>> M
array([[ 0.79463434,  0.60469124,  0.85488643,  0.69161242,  0.25254776],
       [ 0.07024954,  0.84918038,  0.01713536,  0.42620873,  0.97347887],
       [ 0.3374191 ,  0.99535699,  0.79378892,  0.0504229 ,  0.05136649],
       [ 0.73609556,  0.94250215,  0.67322277,  0.49043047,  0.60657825],
       [ 0.71153444,  0.43242926,  0.29726895,  0.2173065 ,  0.38457722]])

Now we take a slice from this matrix, N, holding the neighbors of some central element (x, y)

>>> x, y = 2, 2
>>> N = M[x-1:x+2, y-1:y+2]
>>> N
array([[ 0.84918038,  0.01713536,  0.42620873],
       [ 0.99535699,  0.79378892,  0.0504229 ],
       [ 0.94250215,  0.67322277,  0.49043047]])

We can now get a new matrix showing which of the elements of the orginal matrix M is equal to the max from N

>>> M == N.max()
array([[False, False, False, False, False],
       [False, False, False, False, False],
       [False,  True, False, False, False],
       [False, False, False, False, False],
       [False, False, False, False, False]], dtype=bool)

Now we can use numpy.where to get the index of the element(s) that are True in this matrix. zip those to get a list of tuples.

>>> zip(*np.where(M == N.max()))
[(2, 1)]

Note that those are the positions in the original matrix M, i.e. they could contain tuples that are not in N. Alternatively, you can get just the maximum values in N, but then you'll have to add x-1 and y-1 as offset afterwards.

>>> zip(*np.where(N == N.max()))
[(1, 0)]
tobias_k
  • 81,265
  • 12
  • 120
  • 179
  • Addendum: In case you are not using `numpy` in the rest of your code: You can easily convert to `numpy` and back using `np.array(list_of_lists)` and `list(map(list, numpy_matrix))` – tobias_k Jun 13 '15 at 14:56