1

I have :

A = np.array([[0,1,1],[0,3,2],[1,1,1],[1,5,2]])

where the NumPy array is sorted based on first element and then second element and so on.

I want to insert [1,4,10] into A,such that the output would be :

A = array([[0,1,1],[0,3,2],[1,1,1],[1,4,10][1,5,2]])                 

How should I do it?

1 Answer 1

2

First off, stack the new 1D array as the last row with np.vstack -

B = np.vstack((A,[1,4,10]))

Now, for maintaining the precedence order of considering first and then second and so on elements for each row, assume each row as an indexing tuple and then get the sorted indices. This could be achieved with np.ravel_multi_index(B.T,B.max(0)+1). Then, use these indices for rearranging rows of B and have the desired output. Thus, the final code would be -

out = B[np.ravel_multi_index(B.T,B.max(0)+1).argsort()]

It seems there's an alternative with np.lexsort to get the sorted indices that respects that precedence, but does from in the opposite sense. So, we need to reverse the order of elements row-wise, use lexsort and then get the sorted indices. These indices could then be used for indexing into B just like in the previous approach and get us the output. So, the alternative final code with np.lexsort would be -

out = B[np.lexsort(B[:,::-1].T)]

Sample run -

In [60]: A
Out[60]: 
array([[0, 1, 1],
       [0, 3, 2],
       [1, 1, 1],
       [1, 5, 2]])

In [61]: B = np.vstack((A,[1,4,10]))

In [62]: B
Out[62]: 
array([[ 0,  1,  1],
       [ 0,  3,  2],
       [ 1,  1,  1],
       [ 1,  5,  2],
       [ 1,  4, 10]])  # <= New row

In [63]: B[np.ravel_multi_index(B.T,B.max(0)+1).argsort()]
Out[63]: 
array([[ 0,  1,  1],
       [ 0,  3,  2],
       [ 1,  1,  1],
       [ 1,  4, 10],   # <= New row moved here
       [ 1,  5,  2]])

In [64]: B[np.lexsort(B[:,::-1].T)]
Out[64]: 
array([[ 0,  1,  1],
       [ 0,  3,  2],
       [ 1,  1,  1],
       [ 1,  4, 10],   # <= New row moved here
       [ 1,  5,  2]])
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.