Hi, Suppose an array of shape (N,2,2), that is N arrays of shape (2,2). I want to select an element (x,y) from each one of the subarrays, so I get a 1dimensional array of length N. For instance: In [228]: t=np.arange(8).reshape(2,2,2) In [229]: t Out[229]: array([[[0, 1], [2, 3]], [[4, 5], [6, 7]]]) In [230]: x=[0,1] In [231]: y=[1,1] In [232]: t[[0,1],x,y] Out[232]: array([1, 7]) This way, I get the elements (0,1) and (1,1) which is what I wanted. The question is: is it possible to omit the [0,1] in the index? Thanks in advance.  Ernest
yes use the symbol ':' so you want t[:,x,y] 2010/11/21 Ernest Adrogué <eadrogue@gmx.net>:
Hi,
Suppose an array of shape (N,2,2), that is N arrays of shape (2,2). I want to select an element (x,y) from each one of the subarrays, so I get a 1dimensional array of length N. For instance:
In [228]: t=np.arange(8).reshape(2,2,2)
In [229]: t Out[229]: array([[[0, 1], [2, 3]],
[[4, 5], [6, 7]]])
In [230]: x=[0,1]
In [231]: y=[1,1]
In [232]: t[[0,1],x,y] Out[232]: array([1, 7])
This way, I get the elements (0,1) and (1,1) which is what I wanted. The question is: is it possible to omit the [0,1] in the index?
Thanks in advance.
 Ernest _______________________________________________ NumPyDiscussion mailing list NumPyDiscussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpydiscussion
read about basic slicing : http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html On Sun, Nov 21, 2010 at 11:28 AM, John Salvatier <jsalvati@u.washington.edu> wrote:
yes use the symbol ':'
so you want
t[:,x,y]
2010/11/21 Ernest Adrogué <eadrogue@gmx.net>:
Hi,
Suppose an array of shape (N,2,2), that is N arrays of shape (2,2). I want to select an element (x,y) from each one of the subarrays, so I get a 1dimensional array of length N. For instance:
In [228]: t=np.arange(8).reshape(2,2,2)
In [229]: t Out[229]: array([[[0, 1], [2, 3]],
[[4, 5], [6, 7]]])
In [230]: x=[0,1]
In [231]: y=[1,1]
In [232]: t[[0,1],x,y] Out[232]: array([1, 7])
This way, I get the elements (0,1) and (1,1) which is what I wanted. The question is: is it possible to omit the [0,1] in the index?
Thanks in advance.
 Ernest _______________________________________________ NumPyDiscussion mailing list NumPyDiscussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpydiscussion
On 11/21/10 11:37 AM, Ernest Adrogué wrote:
so you want
t[:,x,y]
I tried that, but it's not the same:
In [307]: t[[0,1],x,y] Out[307]: array([1, 7])
In [308]: t[:,x,y] Out[308]: array([[1, 3], [5, 7]])
what is your t? Here's my example, which I think matches what you asked for: In [1]: import numpy as np In [2]: a = np.arange(12) In [3]: a.shape = (3,2,2) In [4]: a Out[4]: array([[[ 0, 1], [ 2, 3]], [[ 4, 5], [ 6, 7]], [[ 8, 9], [10, 11]]]) In [5]: a[:,1,0] Out[5]: array([ 2, 6, 10])  Christopher Barker, Ph.D. Oceanographer Emergency Response Division NOAA/NOS/OR&R (206) 5266959 voice 7600 Sand Point Way NE (206) 5266329 fax Seattle, WA 98115 (206) 5266317 main reception Chris.Barker@noaa.gov
I didn't realize the x's and y's were varying the first time around. There's probably a way to omit it, but I think the conceptually simplest way is probably what you had to begin with. Build an index by saying i = numpy.arange(0, t.shape[0]) then you can do t[i, x,y] On Mon, Nov 22, 2010 at 11:08 AM, Christopher Barker <Chris.Barker@noaa.gov> wrote:
On 11/21/10 11:37 AM, Ernest Adrogué wrote:
so you want
t[:,x,y]
I tried that, but it's not the same:
In [307]: t[[0,1],x,y] Out[307]: array([1, 7])
In [308]: t[:,x,y] Out[308]: array([[1, 3], [5, 7]])
what is your t? Here's my example, which I think matches what you asked for:
In [1]: import numpy as np
In [2]: a = np.arange(12)
In [3]: a.shape = (3,2,2)
In [4]: a Out[4]: array([[[ 0, 1], [ 2, 3]],
[[ 4, 5], [ 6, 7]],
[[ 8, 9], [10, 11]]])
In [5]: a[:,1,0] Out[5]: array([ 2, 6, 10])
 Christopher Barker, Ph.D. Oceanographer
Emergency Response Division NOAA/NOS/OR&R (206) 5266959 voice 7600 Sand Point Way NE (206) 5266329 fax Seattle, WA 98115 (206) 5266317 main reception
Chris.Barker@noaa.gov _______________________________________________ NumPyDiscussion mailing list NumPyDiscussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpydiscussion
22/11/10 @ 11:20 (0800), thus spake John Salvatier:
I didn't realize the x's and y's were varying the first time around. There's probably a way to omit it, but I think the conceptually simplest way is probably what you had to begin with. Build an index by saying i = numpy.arange(0, t.shape[0])
then you can do t[i, x,y]
Exactly. I was just wondering if I can speed this up by omitting building the "arange array". This is inside a function that gets called a lot, so I suppose it would make a difference if I can get rid of it.  Ernest
22/11/10 @ 11:08 (0800), thus spake Christopher Barker:
On 11/21/10 11:37 AM, Ernest Adrogué wrote:
so you want
t[:,x,y]
I tried that, but it's not the same:
In [307]: t[[0,1],x,y] Out[307]: array([1, 7])
In [308]: t[:,x,y] Out[308]: array([[1, 3], [5, 7]])
what is your t? Here's my example, which I think matches what you asked for:
In [1]: import numpy as np
In [2]: a = np.arange(12)
In [3]: a.shape = (3,2,2)
In [4]: a Out[4]: array([[[ 0, 1], [ 2, 3]],
[[ 4, 5], [ 6, 7]],
[[ 8, 9], [10, 11]]])
In [5]: a[:,1,0] Out[5]: array([ 2, 6, 10])
This works with scalar indices, but not with arrays. The problem is that I don't want always the same element from each subarray, but an arbitrary element, say the (1,0) from the first, the (0,0) from the second, and so on, so I have to use arrays.  Ernest
2010/11/21 Ernest Adrogué <eadrogue@gmx.net>:
Hi,
Suppose an array of shape (N,2,2), that is N arrays of shape (2,2). I want to select an element (x,y) from each one of the subarrays, so I get a 1dimensional array of length N. For instance:
In [228]: t=np.arange(8).reshape(2,2,2)
In [229]: t Out[229]: array([[[0, 1], [2, 3]],
[[4, 5], [6, 7]]])
In [230]: x=[0,1]
In [231]: y=[1,1]
In [232]: t[[0,1],x,y] Out[232]: array([1, 7])
This way, I get the elements (0,1) and (1,1) which is what I wanted. The question is: is it possible to omit the [0,1] in the index?
No, but you can write generic code for it: t[np.arange(t.shape[0]), x, y]  Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth."  Umberto Eco
22/11/10 @ 14:04 (0600), thus spake Robert Kern:
This way, I get the elements (0,1) and (1,1) which is what I wanted. The question is: is it possible to omit the [0,1] in the index?
No, but you can write generic code for it:
t[np.arange(t.shape[0]), x, y]
Thank you. This is what I wanted to know.  Ernest
I think that the only speedup you will get is defining an index only once and reusing it. 2010/11/22 Ernest Adrogué <eadrogue@gmx.net>:
22/11/10 @ 14:04 (0600), thus spake Robert Kern:
This way, I get the elements (0,1) and (1,1) which is what I wanted. The question is: is it possible to omit the [0,1] in the index?
No, but you can write generic code for it:
t[np.arange(t.shape[0]), x, y]
Thank you. This is what I wanted to know.
 Ernest _______________________________________________ NumPyDiscussion mailing list NumPyDiscussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpydiscussion
participants (4)

Christopher Barker

Ernest Adrogué

John Salvatier

Robert Kern