[Numpy-discussion] extracting values from an array

Greg Willden gregwillden at gmail.com
Thu Dec 28 11:38:23 CST 2006

```Hi Eric,
Well I think that you have the parts that you need.
Perhaps something like is what you want.  Put x1 and x2 into an array and
sort it then access it from the sorted array.

x=N.arange(0.,-1.,-0.1);
xs=sort(array([-0.1, -0.55]));
sort(x[(x >= xs[0] )&(x<=xs[1])])

returns:  [-0.5,-0.4,-0.3,-0.2,-0.1,]

x=N.arange(0.,1.,0.1);
xs=sort(array([0.1, 0.55]));
sort(x[(x >= xs[0] )&(x<=xs[1])])

returns: [ 0.1, 0.2, 0.3, 0.4, 0.5,]

Same code just different x and different limits going into xs.
Cheers,
Greg

On 12/28/06, Eric Emsellem <emsellem at obs.univ-lyon1.fr> wrote:
>
>  Hi,
> thanks for the answer, but I guess my request was not clear.
> What I want is something which works in ALL cases so that
>
> function(x, x1, x2) provides the output I mentioned... What you propose
> (as far as I can see) depends on the values of x1, x2, their order and the
> order of x (decreasing, increasing)...
>
> if you have a hint on how to do this without TESTING how x is ordered
> (dec, inc) and which of x1 or x2 is larger...
> thanks
>
> Eric
>
> Greg Willden wrote:
>
> Hi Eric,
> Here are ways of doing this.
> starting with
> import numpy as N
>
> On 12/28/06, Eric Emsellem < emsellem at obs.univ-lyon1.fr> wrote:
> >
> > ### Increasing order in x, and x1 <= x2 :
> > x = arange(0.,1.,0.1)
> > x1 = 0.1
> > x2 = 0.55
> > ### the output I would like is simply: array([ 0.1, 0.2, 0.3, 0.4,
> > 0.5])
>
>
> x=N.arange(0.,1.,0.1)
> x[ (x>=0.1) & (x<= 0.55) ]
>
> ### decreasing order in x, and x1 <= x2 :
> > x = arange(0.,-1.,-0.1)
> > x1 = -0.55
> > x2 = -0.1
> > ### I would like is then: array([ -0.5, -0.4, -0.3, -0.2, -0.1])
>
>
> x=N.arange(0.,-1.,-0.1)
> N.sort( x[ (x<=-0.1) & (x>=-0.55) ] )
> or
> x[(x<=-0.1)&(x>=- 0.55)][::-1]
> just reverses the returned array.
>
>
> ### decreasing order in x, and x1 >= x2 :
> > x = arange(0.,-1.,-0.1)
> > x1 = -0.1
> > x2 = -0.55
> > ### I would like is then: array([ -0.1, -0.2, -0.3, -0.4, -0.5])
>
>
> x=N.arange(0.,-1.,-0.1)
> x[ (x<=-0.1) & (x>=-0.55) ]
>
>
> A few comments because I'm not totally clear on what you want to do.
>  (x<=-0.1)&(x>=-0.55)
> will give you a boolean array of the same length as x
> find((x<=-0.1)&(x>=-0.55))
> will return the list of indices where the argument is true.
>
> Regards,
> Greg
>
> --
> Linux.  Because rebooting is for adding hardware.
>
> ------------------------------
>
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at scipy.orghttp://projects.scipy.org/mailman/listinfo/numpy-discussion
>
>
> --
> ====================================================================
> Eric Emsellem                             emsellem at obs.univ-lyon1.fr
>                            Centre de Recherche Astrophysique de Lyon
> 9 av. Charles-Andre                        tel: +33 (0)4 78 86 83 84
> 69561 Saint-Genis Laval Cedex              fax: +33 (0)4 78 86 83 86
> France                    http://www-obs.univ-lyon1.fr/eric.emsellem
> ====================================================================
>
>
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>
>
>

--
Linux.  Because rebooting is for adding hardware.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/numpy-discussion/attachments/20061228/954c6bcf/attachment-0001.html
```