
suppose I have: In [10]: u Out[10]: array([[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]]) And I have a vector v: v = np.array ((0,1,0,1,0)) I want to form an output vector which selects items from u where v is the index of the row of u to be selected. In the above example, I want: w = [0,6,2,8,4] I can't seem to find a syntax that does this. Now, more importantly, I need the result to be a reference to the original array (not a copy), because I'm going to use it on the LHS of an assignment. Is this possible?

On Fri, Oct 14, 2011 at 7:04 AM, Neal Becker <ndbecker2@gmail.com> wrote:
suppose I have:
In [10]: u Out[10]: array([[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]])
And I have a vector v: v = np.array ((0,1,0,1,0))
I want to form an output vector which selects items from u where v is the index of the row of u to be selected.
In the above example, I want:
w = [0,6,2,8,4]
I can't seem to find a syntax that does this.
Now, more importantly, I need the result to be a reference to the original array (not a copy), because I'm going to use it on the LHS of an assignment. Is this possible?
In [27]: a Out[27]: array([[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]]) In [28]: v = array([0,1,0,1,0]) In [29]: a[v,range(5)] Out[29]: array([0, 6, 2, 8, 4]) In [30]: a[v,range(5)] = 99 In [31]: a Out[31]: array([[99, 1, 99, 3, 99], [ 5, 99, 7, 99, 9]]) In line [29], the result is a copy, *not* a reference. In [30], however, the assignment does write 99 into a. Warren
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion

Le vendredi 14 octobre 2011 à 08:04 -0400, Neal Becker a écrit :
suppose I have:
In [10]: u Out[10]: array([[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]])
And I have a vector v: v = np.array ((0,1,0,1,0))
I want to form an output vector which selects items from u where v is the index of the row of u to be selected.
In the above example, I want:
w = [0,6,2,8,4]
I can't seem to find a syntax that does this.
Now, more importantly, I need the result to be a reference to the original array (not a copy), because I'm going to use it on the LHS of an assignment. Is this possible?
What about np.where? http://docs.scipy.org/doc/numpy/reference/generated/numpy.where.html w = np.where(v, u[1], u[0]) if you may want to have more than two options (more than two lines for u), then np.choose may be more appropriate http://docs.scipy.org/doc/numpy/reference/generated/numpy.choose.html -- Fabrice Silva

Fabrice Silva wrote:
Le vendredi 14 octobre 2011 à 08:04 -0400, Neal Becker a écrit :
suppose I have:
In [10]: u Out[10]: array([[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]])
And I have a vector v: v = np.array ((0,1,0,1,0))
I want to form an output vector which selects items from u where v is the index of the row of u to be selected.
In the above example, I want:
w = [0,6,2,8,4]
I can't seem to find a syntax that does this.
Now, more importantly, I need the result to be a reference to the original array (not a copy), because I'm going to use it on the LHS of an assignment. Is this possible?
What about np.where? http://docs.scipy.org/doc/numpy/reference/generated/numpy.where.html
w = np.where(v, u[1], u[0])
if you may want to have more than two options (more than two lines for u), then np.choose may be more appropriate http://docs.scipy.org/doc/numpy/reference/generated/numpy.choose.html
Will np.choose result in a lval?

On 10/14/11 5:04 AM, Neal Becker wrote:
suppose I have:
In [10]: u Out[10]: array([[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]])
And I have a vector v: v = np.array ((0,1,0,1,0))
I want to form an output vector which selects items from u where v is the index of the row of u to be selected.
Now, more importantly, I need the result to be a reference to the original array (not a copy), because I'm going to use it on the LHS of an assignment. Is this possible?
No, it's not. numpy arrays need to be describable with regular strides -- when selecting arbitrary elements from an array, there is no way to describe the resulting array as regular strides into the same data block as the original. -Chris -- Christopher Barker, Ph.D. Oceanographer Emergency Response Division NOAA/NOS/OR&R (206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception Chris.Barker@noaa.gov
participants (5)
-
Chris.Barker
-
Fabrice Silva
-
Jean-Luc Menut
-
Neal Becker
-
Warren Weckesser