0

I am learning Python and I have the following doubt about the NumPy library:

I have the following 2x2 matrix:

a = np.array([[2,4],[5,1]])

This matrix gives the following output:

[[2 4]  [5 1]]

Nevertheless, I would like to reorder the elements to obtain:

[[5 1]  [2 4]]
Ajean
  • 5,528
  • 14
  • 46
  • 69
David
  • 45
  • 2
  • 9

2 Answers2

2

If you mean reverse the order, just use a[::-1].

If you mean swapping: a[1], a[0] = a[0], a[1].copy(). Here you need the .copy() since slicing a Numpy array only creates a view, not copying the data.

Tuwuh S
  • 291
  • 1
  • 7
0

Just use np.flipud(a)! Your question is kind of a duplicate of this question. Your question is also easily answered if you just search documentation for basic numpy functions (see documentation for numpy.flipud). I realize you may be a beginner ( keep learning :) ), but stack overflow isn't really intended for such simple questions, it's for questions that are not answered in any documentation. So next time you're not sure I suggest searching through python documentation or just look at previously answered stack overflow questions.

Bow
  • 987
  • 1
  • 10
  • 19