如何在Matlab中使用另一个向量中的值对矩阵进行排序? [英] How to sort a matrix by using values in another vector in matlab?
本文介绍了如何在Matlab中使用另一个向量中的值对矩阵进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的情况是:
我得到一个1000 * 2矩阵和一个1000 * 1向量.
I get a 1000*2 matrix and a 1000*1 vector.
然后矩阵中的行i映射到向量中的元素i.
And the row i in the matrix is mapped to the element i in the vector.
向量中的元素都是整数.
And the elements in the vector are all integer.
现在我要从低到高对向量中的元素进行排序.
Now I want to sort the elements in the vector from low to high.
我想用新向量的序列得到一个新矩阵.且映射关系与原始情况相同.
And I want to get a new matrix with the sequence of the new vector. And the mapping relationships are equal to the original situation.
如何在Matlab中做到这一点?
How to do this in Matlab?
谢谢!
推荐答案
使用 <代码>排序代码> :
首先将向量连接到矩阵:
First concat your vector to your matrix:
M2 = [V, M];
然后对行进行排序:
M2 = sortrows(M2); %// You should just do sortrows([V, M]) here, I just split it for the explanation
然后分割向量和矩阵:
V_sorted = M2(:,1);
M_sorted = M2(:, 2:end);
或者您也可以使用 sort
:
Or alternatively you can use the second output from sort
:
[V_sorted, newRowOrder] = sort(V);
M_sorted = M(newRowOrder, :);
这篇关于如何在Matlab中使用另一个向量中的值对矩阵进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文