Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vue.js/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python等价于Matlab[a,b]=sort(y)_Python_Matlab_Python 2.7 - Fatal编程技术网

Python等价于Matlab[a,b]=sort(y)

Python等价于Matlab[a,b]=sort(y),python,matlab,python-2.7,Python,Matlab,Python 2.7,我对Python语言相当陌生,想知道如何 (1) y = [some vector] (2) z = [some other vector] (3) [ynew,indx] = sort(y) (4) znew = z(indx) 我可以做第1、2和4行,但第3行让我很舒服。任何建议。我想要的不是用户编写的函数,而是语言本身固有的功能。 感谢对第3行使用NumPy,假设y是行向量,否则需要axis=0: ynew=y.sort(axis=1) indx=y.argsort(axis=1

我对Python语言相当陌生,想知道如何

(1)  y = [some vector]
(2)  z = [some other vector]
(3)  [ynew,indx] = sort(y)
(4)  znew = z(indx)
我可以做第1、2和4行,但第3行让我很舒服。任何建议。我想要的不是用户编写的函数,而是语言本身固有的功能。

感谢对第3行使用NumPy,假设
y
是行向量,否则需要
axis=0

ynew=y.sort(axis=1)
indx=y.argsort(axis=1)

您可以尝试执行以下操作:

import numpy as np

y = [1,3,2]
z = [3,2,1]
indx = [i[0] for i in sorted(enumerate(y), key=lambda x:x[1])]
print(indx)

#convert z to numpy array in order to use np.ix_ function
z = np.asarray(z)

znew = z[np.ix_(indx)]
print(znew)
结果:

#the indx is
[0, 2, 1]

#the znew is
array([3, 1, 2])

我知道必须有一个命令才能找到数组的索引。谢谢