MATLAB Answers

How to sort a two column vector

6 views (last 30 days)
Jórdan Venâncio Leite
Jórdan Venâncio Leite on 2 Jun 2020
Commented: Stephen Cobeldick on 11 Jun 2020
This question was flagged by Stephen Cobeldick
I need to sort a two column vector

  3 Comments

dpb
dpb on 2 Jun 2020
s=sortrows(x);
dpb
dpb on 2 Jun 2020
Of course it worked... <VERY big grin> ...
Stephen Cobeldick
Stephen Cobeldick on 11 Jun 2020
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.

Sign in to comment.

Answers (2)

dpb
dpb on 2 Jun 2020
s=sortrows(x);

  0 Comments

Sign in to comment.


KSSV
KSSV on 2 Jun 2020
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

  0 Comments

Sign in to comment.

Tags