How to sort a two column vector
显示 更早的评论
I need to order this two column vector (attached) in such a way that the first column (x) is ordered ascending and the second column follows this order. In other words, it is very important to me that the values in the second column remain the same for each value of x.
Thanks in advance.
4 个评论
dpb
2020-6-2
s=sortrows(x);
dpb
2020-6-2
Of course it worked... <VERY big grin> ...
Stephen23
2020-6-11
Original question by Jórdan Venâncio Leite on 2nd June 2020 retrieved from Google Cache (uploaded data could not be retrieved):
"How to sort a two column vector"
I need to order this two column vector (attached) in such a way that the first column (x) is ordered ascending and the second column follows this order. In other words, it is very important to me that the values in the second column remain the same for each value of x.
Thanks in advance.
Rena Berman
2020-10-12
(Answers Dev) Restored edit
回答(2 个)
KSSV
2020-6-2
Let A be your vector of two columns.
[val,idx] = sort(A(:,1)) ; % sort the first column
iwant = A(idx,:) % arrange A in ascending order of first column
类别
在 帮助中心 和 File Exchange 中查找有关 Shifting and Sorting Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!