How to sort the components of a vector?

1 visualizzazione (ultimi 30 giorni)
Andre Ged
Andre Ged il 19 Feb 2016
Modificato: Stephen23 il 19 Feb 2016
How can i sort the components of a vector in increasing order?

Risposta accettata

Stephen23
Stephen23 il 19 Feb 2016
Modificato: Stephen23 il 19 Feb 2016
Use sort
sort(vec)
  2 Commenti
Andre Ged
Andre Ged il 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 il 19 Feb 2016
Modificato: Stephen23 il 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.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Shifting and Sorting Matrices in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by