I have a big matrix in python (in the order of 2x500), but for this example let's assume it is a 2x4 matrix:
matrix1=np.array[[2,2,3,2],
[2,-1,-1,1]]
The matrix is a collection of points in the plain, ie:
matrix1=[[x_1,x_2,x_3,x_4],
[y_1,y_2,y_3,y_4]]
I want to sort it in such a way that the points are ordered first by the lowest to highest value of y and then in case the y value is the same, to be sorted by x value (to say points should be going from bottom to top and in case the y value is the same from left to right in the plane). ie the matrix1 should output:
matrix1sorted=[[2,3,2,2],
[-1,-1,1,2]]
So far I was able to sort it by the y component as follows:
import numpy as np
matrix1=np.array([[2,3,2,2],
[2,-1,-1,1]])
ind = np.argsort( matrix1[1,:] )
matrix1sorted=matrix1[:,ind]
which outputs:
matrix1sorted=[[3,2,2,2],
[-1,-1,1,2]]
I have also tried something along the lines of:
matrix1=np.array([[2,3,2,2],
[2,-1,-1,1]])
ind = np.argsort( matrix1[1,:] )
matrix1sorted=matrix1[:,ind]
c=0
for i in range(len(matrix1[0,:])-1):
if matrix1sorted[0,i]==matrix1sorted[0,i+1]:
c=c+1
else:
ind=np.argsort( matrix1sorted[0,i:c+1] )
matrix1sorted[:,i:c+1]=matrix1sorted[:,ind]
c=0
Which idea was to sort first by the y component and then by the x component if the y component is the same, but I am having some trouble. Any help would be appreciated.