How to sort the components of a vector?

2 views (last 30 days)
How can i sort the components of a vector in increasing order?

Accepted Answer

Stephen23
Stephen23 on 19 Feb 2016
Edited: Stephen23 on 19 Feb 2016
Use sort
sort(vec)
  2 Comments
Andre Ged
Andre Ged on 19 Feb 2016
Thank you! Another little question. If every elements of that vector refers to a row of a matrix, is there a way to sort the rows of this matrix like the vector? I hope it is well explained!
Stephen23
Stephen23 on 19 Feb 2016
Edited: Stephen23 on 19 Feb 2016
Use the second output of sort as indices for the matrix rows:
[newvec,idx] = sort(vec)
out = mat(idx,:)
This will sort the matrix rows into the same order as the vector is sorted.

Sign in to comment.

More Answers (0)

Categories

Find more on Shifting and Sorting Matrices in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!