0

Assume I have the following numpy array :

a = np.array([[4, 5, 8],
              [7, 2, 9],
              [1, 5, 3]])

and I want to extract points from the array 'a' to have this array :

b = array([[4, 8],
           [1, 3]])

How can I do this ?

PS : In my real case I have 13*13 matrix and I want to create a 3*3 matrix from the first one

3
  • 13x13 -> 3x3: what are the criteria to select the elements for the output? Commented Feb 1, 2020 at 17:18
  • I have a function and I want to extract the points that the user want, let's say the user execute the function like this: my_function([1, 3, 5]), then I create the cartesian power from these points, namely: {(1,1), (1,3), ..., (5, 5)} and I extract all these points from my 13*13 matrix to create a 3*3 matrix Commented Feb 1, 2020 at 17:34
  • I think Ethan's answer has what you need ;-) Commented Feb 1, 2020 at 17:43

1 Answer 1

2

You can use np.ix_() for this to create a map of which values you want by location.

>>> a = np.arange(1,10).reshape(3,3)
>>> a
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])

>>> b=np.ix_([0,2],[0, 2])
>>> a[b]
array([[1, 3],
       [7, 9]])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.