Python (Numpy) array sorting -
i've got array, named v, of dtype('float64'):
array([[ 9.33350000e+05, 8.75886500e+06, 3.45765000e+02], [ 4.33350000e+05, 8.75886500e+06, 6.19200000e+00], [ 1.33360000e+05, 8.75886500e+06, 6.76650000e+02]])
... i've acquired file using np.loadtxt command. sort after values of first column, without mixing structure keeps numbers listed on same line together. using v.sort(axis=0) gives me:
array([[ 1.33360000e+05, 8.75886500e+06, 6.19200000e+00], [ 4.33350000e+05, 8.75886500e+06, 3.45765000e+02], [ 9.33350000e+05, 8.75886500e+06, 6.76650000e+02]])
... i.e. places smallest number of third column in first line, etc. rather want this...
array([[ 1.33360000e+05, 8.75886500e+06, 6.76650000e+02], [ 4.33350000e+05, 8.75886500e+06, 6.19200000e+00], [ 9.33350000e+05, 8.75886500e+06, 3.45765000e+02]])
... elements of each line hasn't been moved relatively each other.
try
v[v[:,0].argsort()]
(with v
being array). v[:,0]
first column, , .argsort()
returns indices sort first column. apply ordering whole array using advanced indexing. note sorte copy of array.
the way know of sort array in place use record dtype:
v.dtype = [("x", float), ("y", float), ("z", float)] v.shape = v.size v.sort(order="x")
Comments
Post a Comment