For example, given a python numpy.ndarray a = array([[1, 2], [3, 4], [5, 6]]), I want to select the 0th and 2nd row of array a into a new array b, such that b becomes array([[1,2],[5,6]].
I need to solution to work on more general problems, where the original 2d array can have more rows and I should be able to select the rows based on some disjoint ranges. In general, I was looking for something like a[i:j] + a[k:p] that works for 1-d list, but it seems 2d-arrays won’t add up this way.
update
It seems that I can use vstack((a[i:j], a[k:p])) to get this working, but is there any elegant way to do this?
You can use list indexing:
More generally, to select rows
i:jandk:p(I’m assuming in the python sense, meaning rows i to j but not including j):Note that the
range(i,j) + range(k,p)creates a flat list of[ i, i+1, ..., j-1, k, k+1, ..., p-1 ], which is then used to index the rows ofa.