# [SciPy-User] Selecting elements in a numpy array by their indices contained in two arrays

5 messages
Open this post in threaded view
|
Report Content as Inappropriate

## [SciPy-User] Selecting elements in a numpy array by their indices contained in two arrays

 Hi list.   Suppose I have a numpy array A (4,4) and two 1D array, one (X1) that would contain (2,2,1,1) and the other one X2 that would be (3,3,3,3). Is there a way to select all the elements of A that have indices between X1 and X2? In the current case, that would mean selecting A[2:3,0], A[2:3,1], A[1:3,2] and A[1:3,3]   Thanks. Éric.   -- Un clavier azerty en vaut deux ---------------------------------------------------------- Éric Depagne [hidden email]  _______________________________________________ SciPy-User mailing list [hidden email] https://mail.python.org/mailman/listinfo/scipy-user
Open this post in threaded view
|
Report Content as Inappropriate

## Re: Selecting elements in a numpy array by their indices contained in two arrays

 Le lundi 26 juin 2017, 04:27:47 SAST Éric Depagne a écrit : > Hi list. Hi list,   Some more details on my problem. I know that I can select the elements individually through a for loop, and that will provide me with the extraction I need, but I was wondering if there was a way to do that without a loop. Repeating 4 times A[X1[i]:X2[i],1] will work fine, but I was looking for something like A[X1:X2, 1]   Since in my problem, A is (4000x2000), X1 and X2 (2000,1), avoiding unnecessary loops will make a significant difference.   Thanks. Éric. > > Suppose I have a numpy array A (4,4) and two 1D array, one (X1) that would > contain (2,2,1,1) and the other one X2 that would be (3,3,3,3). > Is there a way to select all the elements of A that have indices between X1 > and X2? In the current case, that would mean selecting A[2:3,0], A[2:3,1], > A[1:3,2] and A[1:3,3] > > Thanks. > Éric.     -- Un clavier azerty en vaut deux ---------------------------------------------------------- Éric Depagne [hidden email]  _______________________________________________ SciPy-User mailing list [hidden email] https://mail.python.org/mailman/listinfo/scipy-user
Open this post in threaded view
|
Report Content as Inappropriate

## Re: Selecting elements in a numpy array by their indices contained in two arrays

 On Mon, Jun 26, 2017 at 4:40 PM, Éric Depagne <[hidden email]> wrote:>> Le lundi 26 juin 2017, 04:27:47 SAST Éric Depagne a écrit :>> > Hi list.>> Hi list,>> Some more details on my problem.>> I know that I can select the elements individually through a for loop, and that will provide me with the extraction I need, but I was wondering if there was a way to do that without a loop.>> Repeating 4 times A[X1[i]:X2[i],1] will work fine, but I was looking for something like> A[X1:X2, 1]There isn't one since for each `i`, there will be a different number of items in the slice, so the result would not be able to be fit into an array with a uniform shape. The loop is the right way to do this.--Robert Kern _______________________________________________ SciPy-User mailing list [hidden email] https://mail.python.org/mailman/listinfo/scipy-user
Open this post in threaded view
|
Report Content as Inappropriate

## Re: Selecting elements in a numpy array by their indices contained in two arrays

 Well, the resulting array size is known ahead of time. I haven’t come up with a nice vectorised NumPy way to do it, but I would recommend preallocating the result array and then using Numba or Cython to loop. You’ll get some pretty nice speed. Also, since you are grabbing column-wise chunks, if possible, I would suggest making sure your A matrix has order=‘F’. (Incidentally, if you want to keep track of the origin columns, I think scipy.sparse.csc_matrix is a nice format for the result.) Juan. On 27 Jun 2017, 9:48 AM +1000, Robert Kern <[hidden email]>, wrote: On Mon, Jun 26, 2017 at 4:40 PM, Éric Depagne <[hidden email]> wrote: > > Le lundi 26 juin 2017, 04:27:47 SAST Éric Depagne a écrit : > > > Hi list. > > Hi list, > > Some more details on my problem. > > I know that I can select the elements individually through a for loop, and that will provide me with the extraction I need, but I was wondering if there was a way to do that without a loop. > > Repeating 4 times A[X1[i]:X2[i],1] will work fine, but I was looking for something like > A[X1:X2, 1] There isn't one since for each `i`, there will be a different number of items in the slice, so the result would not be able to be fit into an array with a uniform shape. The loop is the right way to do this. -- Robert Kern _______________________________________________ SciPy-User mailing list [hidden email] https://mail.python.org/mailman/listinfo/scipy-user _______________________________________________ SciPy-User mailing list [hidden email] https://mail.python.org/mailman/listinfo/scipy-user