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.