-1

I'm dealing with a longitude array named LON, but I encountered some problems with the numpy.where() function.

>>> print LON[777,777]
13.4635573678
>>> print np.where(LON == 13.4635573678)[0]
[]
>>> print np.where(LON == 13.4635573678)[1]
[]

It doesn't find the LON entry where the array is equal to a value that certainly exists. Is the problem related to the fact I'm dealing with double variables? Because until now np.where() always worked fine for both integers, floats and strings...

JRodDynamite
  • 12,325
  • 5
  • 43
  • 63
urgeo
  • 645
  • 1
  • 9
  • 19
  • 3
    13.4635573678 is not in your array. The print statement does not display all digits. Try `np.where(LON == LON[777, 777])[0]`. – timgeb Feb 24 '16 at 15:00
  • Yes, it gives me back the right answer... but it's kind of a dog biting its tail :) How do I get all digits of my LON[777,777] entry? – urgeo Feb 24 '16 at 15:02
  • Why would you *want* all digits? – timgeb Feb 24 '16 at 15:05
  • You could try `np.where(abs(LON - 13.4635573678) < 0.01)` or something like that. – tobias_k Feb 24 '16 at 15:11
  • In order to get the entry. I mean, I have a longitude value (e.g., 13.46): I wrote a function for retrieving the nearest value to 13.4 within the LON array and this is 13.4635573678. Now I need to find the position of this value inside my array, i.e. [777,777]. – urgeo Feb 24 '16 at 15:12
  • Why don't you rewrite that function to return you both the value and its index? – tobias_k Feb 24 '16 at 15:12
  • @tobias_k this could be a nice idea, thanks! – urgeo Feb 24 '16 at 15:13
  • @tobias_k because mostly I don't know how to get the index related to a certain value without using the where function... – urgeo Feb 24 '16 at 15:13
  • `float` comparison is always tricky... i see that you are using python 2. starting from python 3.5 you can use [math.isclose](https://docs.python.org/3/whatsnew/3.5.html#pep-485-a-function-for-testing-approximate-equality) to compare `float`s. – hiro protagonist Feb 24 '16 at 15:20
  • thanks for the tip @hiroprotagonist, but unfortunately I need to use version 2.7 for other reasons... – urgeo Feb 24 '16 at 15:25

1 Answers1

3

One way to work around this might be to use np.where with an approximate match:

>>> X = np.linspace(1, 10, 100).reshape((10,10))
>>> np.where(abs(X - 6.3) < 0.1)
(array([5, 5]), array([8, 9]))
>>> X[np.where(abs(X - 6.3) < 0.1)]
array([ 6.27272727,  6.36363636])

Of course, this could give you more than one match, if the epsilon (0.1 in this example) is too large, but the same could be the case when using an exact match, in case there are multiple entries in the array with the same coordinate.

Edit: as pointed out in comments, you could also use np.isclose, even with Python 2.7 where math.isclose is not available. Note, however, that np.isclose will not give an array of coordinates, but an array of True/False. If you need the coordinates, you could pipe the result of np.close through np.where again.

>>> np.where(np.isclose(X, 6.3636))
(array([5]), array([9]))
>>> X[np.isclose(X, 6.3636)]
array([ 6.36363636])

Alternatively, you could consider changing the function that gave you that coordinate from the LON array to also return the position of that value in the array. This way, you would not need to use np.where at all.

Community
  • 1
  • 1
tobias_k
  • 81,265
  • 12
  • 120
  • 179