how to sort the element of one column in a 220*3 matrix in ascending mode, keeping the elements of other columns dependent on the elements of sorted column ?
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Joseph
am 20 Jul. 2015
Bearbeitet: Stephen23
am 20 Jul. 2015
i have a 220*3 matrix. the elements of first column in this matrix is related to the elements of other 2 columns. how can i sort the elements of first column in ascending mode, moving the elements of other columns correlated to the elements of first column?
0 Kommentare
Akzeptierte Antwort
Stephen23
am 20 Jul. 2015
Bearbeitet: Stephen23
am 20 Jul. 2015
If you want to move the entire row based on how the first column is sorted, then use sortrows with its second optional argument (the rows move along with the first column):
sortrows(X,1)
For example:
>> X = [9,1,1,1;1,2,2,2;5,3,3,3]
X =
9 1 1 1
1 2 2 2
5 3 3 3
>> sortrows(X,1)
ans =
1 2 2 2
5 3 3 3
9 1 1 1
If you want to sort only the first column then just use sort and the appropriate indexing (the other columns do not change):
X(:,1) = sort(X(:,1));
Weitere Antworten (1)
Jan
am 20 Jul. 2015
And this happens inside sortrows:
X = rand(220, 3);
[Y, Order] = sort(X(:, 1));
R = X(Order, :);
0 Kommentare
Siehe auch
Kategorien
Mehr zu Shifting and Sorting Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!