[AstroPy] How can I take the index of "True" value and minimum "True" in list with python ?

Trey Wenger tvw2pu at virginia.edu
Tue Apr 8 10:09:33 EDT 2014


Hi Thuong,

I would do this

index = abs(gE0[:,0] - ek).argmin()

.argmin() returns the index at which the previous argument is smallest.
Now, gE0[;,0][index] will be the value of gE0[;,0] closest to ek.

For example,

IPy> x = np.array([1.1, 2.3, 4.4])
IPy> y = 2.2
IPy> index = np.abs(x-y).argmin()
IPy> index
Out> 1
IPy> x[index]
Out> 2.3
IPy> # this is the value in x closest to y

Cheers,
Trey


On Tue, Apr 8, 2014 at 9:57 AM, THƯỜNG Hoàng Đức <hoangthuong.hust at gmail.com
> wrote:

> Dear AstroPyer,
>
> I have problem with ideals !
> Firstly, I have two array. "ek" (one dimension - value in logspace) and
> "gE0[]" (two dimension - value in logspace). After that I scan value ek in
> array gE0 to find the equal value, It mean that abs(ek-gE0[:,0]) = 0. Due
> to the numerical that in logspace, it can not equal zeros(There are some
> point near the 0), and then I put the error for example err < 0.08 >>>>
> abs(ek-gE0[:,0]) < 0.08. You can see the value below. The result I would to
> take is True but it has several value < 0.08 also true, then how can i take
> smallest if have several True.
>
>
> In [53]: abs(ek-gE0[:,0]) < 0.08
> Out[53]: array([False,  True, False, False, False, False,  True,  True,
> True,  True], dtype=bool)
>
> ek = 0.1
> In [52]: gE0[:,0]
> Out[52]:
> array([ 0.01      ,  0.04641589,  0.21544347,  1.        ,  1.93593016,
>         0.51306049,  0.15479754,  0.06105322,  0.04410808,  0.04849499])
>
> How can i take only the value: i = 1, value = 0.04641589 and i = 9, value
> = 0.04410808
>
>
> Thankyou very muck
>
>  Merci beaucoup,
>
> Thuong
>
> ================================================
> THUONG Hoang Duc
>
> Université des Sciences et des Technologies de Hanoi(USTH)
> University of Science and Technology of Hanoi(USTH)
> Hanoi University of Science and Technology (HUST)
>
> Email: hoangthuong.hust at gmail.com
> Tel: +33 06 52 92 00 96
> Tel: +84 01692887738
>
>
> _______________________________________________
> AstroPy mailing list
> AstroPy at scipy.org
> http://mail.scipy.org/mailman/listinfo/astropy
>
>


-- 
Trey V. Wenger
Department of Astronomy
University of Virginia
http://www.astro.virginia.edu/~tvw2pu/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/astropy/attachments/20140408/eebe7513/attachment.html>


More information about the AstroPy mailing list