Re: [Numpy-discussion] getting indices for array positions

2007-02-08 Thread Christian Meesters
Hi

Thanks for all your suggestions.

Christian
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] getting indices for array positions

2007-02-07 Thread Christian
Christian Meesters  uni-mainz.de> writes:

> Since searchsorted returns the index of the first item in a that is >= or > 
> the key, it can't make the distinction between 0.1 and 0.2 as I would like to 

Then how about a.searchsorted(val+0.5) 

Christian



___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] getting indices for array positions

2007-02-07 Thread Stefan van der Walt
On Wed, Feb 07, 2007 at 02:00:52PM +0100, Christian Meesters wrote:
> This questions might seem stupid, but I didn't get a clever solution myself, 
> or found one in the archives, the cookbook, etc. . If I overlooked something, 
> please give a pointer.
> 
> Well, if I have an 1D array like
> [ 0. ,  0.1,  0.2,  0.3,  0.4,  0.5]
> ,a scalar like 0.122 and want to retrieve the index postion of the closest 
> value of the scalar in the array: Is there any fast method to get
> this?

If I understand correctly:

data = N.array([ 0. ,  0.1,  0.2,  0.3,  0.4,  0.5])
diff = N.abs(data - val)
print N.argmin(diff)

Regards
Stéfan
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] getting indices for array positions

2007-02-07 Thread Robert Cimrman
Christian Meesters wrote:
>> Try searchsorted.
> Thanks, but that doesn't work. Sorry, if my question wasn't clear.
> 
> To illustrate the requirement:
> For instance:
 a
> array([ 0. ,  0.1,  0.2,  0.3,  0.4])
 # should be 1
> ...
 a.searchsorted(0.11)
> 2
 # should be 2
> ...
 a.searchsorted(0.16)
> 2
> 
> I could correct for one index position, of course, but I still have the 
> requirement to get the index of the item with the closest value to the key. 
> Since searchsorted returns the index of the first item in a that is >= or > 
> the key, it can't make the distinction between 0.1 and 0.2 as I would like to 
> have.

I see. But it gives you the index (say 'ii') of the first item that is
grater than your scalar - then you just have to compare your scalar with
a[ii] and a[ii-1] and choose whichever is closer, no?

r.
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] getting indices for array positions

2007-02-07 Thread Christian Meesters
> Try searchsorted.
Thanks, but that doesn't work. Sorry, if my question wasn't clear.

To illustrate the requirement:
For instance:
>>> a
array([ 0. ,  0.1,  0.2,  0.3,  0.4])
>>> # should be 1
...
>>> a.searchsorted(0.11)
2
>>> # should be 2
...
>>> a.searchsorted(0.16)
2

I could correct for one index position, of course, but I still have the 
requirement to get the index of the item with the closest value to the key. 
Since searchsorted returns the index of the first item in a that is >= or > 
the key, it can't make the distinction between 0.1 and 0.2 as I would like to 
have.

Hope this clarifies my question.
 Christian
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] getting indices for array positions

2007-02-07 Thread Robert Cimrman
Christian Meesters wrote:
> Hi
> 
> This questions might seem stupid, but I didn't get a clever solution myself, 
> or found one in the archives, the cookbook, etc. . If I overlooked something, 
> please give a pointer.
> 
> Well, if I have an 1D array like
> [ 0. ,  0.1,  0.2,  0.3,  0.4,  0.5]
> ,a scalar like 0.122 and want to retrieve the index postion of the closest 
> value of the scalar in the array: Is there any fast method to get this?

Try searchsorted.

r.

___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


[Numpy-discussion] getting indices for array positions

2007-02-07 Thread Christian Meesters
Hi

This questions might seem stupid, but I didn't get a clever solution myself, 
or found one in the archives, the cookbook, etc. . If I overlooked something, 
please give a pointer.

Well, if I have an 1D array like
[ 0. ,  0.1,  0.2,  0.3,  0.4,  0.5]
,a scalar like 0.122 and want to retrieve the index postion of the closest 
value of the scalar in the array: Is there any fast method to get this?
Right now I've implemented the following method:

def _get_value_index(value, a):
mindiff = 1e20
index = 0

for intensity, temp_index in zip(a, xrange(a.shape[0])):
diff = abs(intensity - value)
#closer to given value?
if diff <= mindiff:
mindiff = diff
index = temp_index
return index

It works, but is akward and takes too much time (I've no benchmark), if the 
array is long and the method called often within a different function. But it 
should help clarify the problem.

TIA
Christian
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion