0

I want to comparing 2 values in a 2-dimensional numpy array. The array is as follows:

a = [[1, 3, 5],
     [4, 8, 1]]

I want to comparing [1, 3, 5] with [4, 8, 1] with a greater value into 1 group. The result I want is like this:

a1 = [4, 8, 5]
a2 = [1, 3, 1]

How could the code be written in python?

1
  • Please be clear with your example. What you have shown is not an array and has nothing to do with Numpy, yet you have tagged the question numpy. Commented May 1, 2021 at 15:36

2 Answers 2

1

You can use np.sort on axis 0 (column-wise). Reverse the order using [::-1] to get them in descending order

>>> np.sort(a, axis = 0)[::-1]

array([[4, 8, 5],
       [1, 3, 1]])
Sign up to request clarification or add additional context in comments.

Comments

0

Since you only have 2 rows you can do this:

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

idx = np.greater(*a)

a1 = a[idx.astype(int),np.arange(3)]
a2 = a[~idx.astype(int),np.arange(3)]

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.