0

I have a numpy array with three columns and hundres of rows. I want to sort it based on two columns and in cases also regard the third column in my sorting. This is my input (they are x, y and z coordinates):

my_point=np.array([[1., 2., 90.9],
                   [1., 0., 100.1],
                   [1.8, 0., 2.8],
                   [1.8, 2., 3.1],
                   [1.8, 1., 2.7],
                   [3., 0., 3.],
                   [3., 2., 2.9],
                   [2., 1., 100.],
                   [2., 0., 100.]])

Firstly, I want to sort it based on the first (x) and second column (y) and secondly modify this sort based on the third column (z). The key issue is that I must sort my data based on x and y and cannot change it to x and z or y and z, because then it changes the order of all the points. This is my sorting code:

result_array=my_point[np.lexsort((my_point[:,1],my_point[:,0]))]

It gives me:

array([[1., 0., 100.1],
       [1., 2., 90.9],
       [1.8, 0., 2.8],
       [1.8, 1., 2.7],
       [1.8, 2., 3.1],
       [2., 0., 100.],
       [2., 1., 100.],
       [3., 0., 3. ],
       [3., 2., 2.9]])

It is correct but I want to modify my sort based on the third column. I want to consider the values of this column, when it is highly different from the adjacent rows. As result_array shows, third column of its second, third and fourth rows ([1.8, 0., 2.8], [1.8, 1., 2.7] and [1.8, 2., 3.1]) have very low values compared to the next two rows. The x value of these rows is just a little bit higher than next ones and in such cases I want to neglect it and give priority to z values:

array([[1., 0., 100.1],
       [1., 2., 90.9],
       [2., 0., 100.],
       [2., 1., 100.],
       [1.8, 0., 2.8],
       [1.8, 1., 2.7],
       [1.8, 2., 3.1],
       [3., 0., 3. ],
       [3., 2., 2.9]])

For more clarity, I have uploaded a fig showing my desired order of points in 3d space. I think It can be done maybe through defining a function for sorting but I have no idea on how to do it. In advance, I do appreciate any help. enter image description here

1 Answer 1

1

You could define a sort function like

sortfunc = my_point[:, 0] * 100 + my_point[:, 1] * 10 - my_point[:, 2]

idx = np.argsort(sortfunc)
my_point[idx]
# array([[  1. ,   0. , 100.1],
#        [  1. ,   2. ,  90.9],
#        [  2. ,   0. , 100. ],
#        [  2. ,   1. , 100. ],
#        [  1.8,   0. ,   2.8],
#        [  1.8,   1. ,   2.7],
#        [  1.8,   2. ,   3.1],
#        [  3. ,   0. ,   3. ],
#        [  3. ,   2. ,   2.9]])

Of course the coefficients 100, 10, and -1 are then a matter of tuning.

Sign up to request clarification or add additional context in comments.

3 Comments

Dear @Nils Werner, Sorry for my late feed back. Since yesterday I was working with my data. Your method as completely fine but with the new data, I have a big problem and cannot tune the coeficients at all. Do you know any other way to do it.
No, but I suggest you just open another question with the new data and problems.
Dear @Nils Werner, Thanks for your help. I do appreciate it.

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.