【发布时间】:2017-07-18 22:08:27
【问题描述】:
假设我在 matlab 中有一个矩阵 a 和一个向量 b,如下所示:
a =
2 1 1
3 3 1
3 2 2
b =
1 3 2
使用matlab的sort函数我可以实现以下:
[n idx] = sort(b)
n =
1 2 3
idx =
1 3 2
anew = a(idx,idx)
anew =
2 1 1
3 2 2
3 1 3
现在,我想在 python 中做同样的事情。我的尝试:
a = np.array([[2,1,1],[3,3,1],[3,2,2]])
b = [0,2,1]
idx = [i[0] for i in sorted(enumerate(b), key=lambda x:x[1])]
问题是我找不到像使用 Matlab 那样构建anew 矩阵的方法。我试过了:
anew=a[idx]
anew
array([[2, 1, 1],
[3, 2, 2],
[3, 3, 1]])
如您所见,结果(matlab vs python)不一样。
有什么建议吗?
【问题讨论】:
标签: python arrays matlab sorting indexing