Sort a matrix according to only one column [duplicate]
This question already has an answer here:
Sorting entire matrix according to one column in matlab
2 answers
I have this matrix A,
A= [10 -12 4 8;
1 3 11 -2;
1 -9 8 0;
1 1 9 3].
if I use the sortrows function in Matlab B = sortrows(A). I will get this.
B = [1 -9 8 0;
1 1 9 3;
1 3 11 -2;
10 -12 4 8].
The answer I wanted is this.
B = [1 3 11 -2;
1 -9 8 0;
1 1 9 3;
10 -12 4 8]
The thing is, I want to sort my rows here but only based on column 1. if values in column 1 are same, then don't sort them according to column 2(which in genreal this function perform).
I really appreciate, if anyone can help me with this.
Thank you.
matlab sorting
marked as duplicate by obchardon, Community♦ Nov 21 '18 at 8:27
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Sorting entire matrix according to one column in matlab
2 answers
I have this matrix A,
A= [10 -12 4 8;
1 3 11 -2;
1 -9 8 0;
1 1 9 3].
if I use the sortrows function in Matlab B = sortrows(A). I will get this.
B = [1 -9 8 0;
1 1 9 3;
1 3 11 -2;
10 -12 4 8].
The answer I wanted is this.
B = [1 3 11 -2;
1 -9 8 0;
1 1 9 3;
10 -12 4 8]
The thing is, I want to sort my rows here but only based on column 1. if values in column 1 are same, then don't sort them according to column 2(which in genreal this function perform).
I really appreciate, if anyone can help me with this.
Thank you.
matlab sorting
marked as duplicate by obchardon, Community♦ Nov 21 '18 at 8:27
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Sorting entire matrix according to one column in matlab
2 answers
I have this matrix A,
A= [10 -12 4 8;
1 3 11 -2;
1 -9 8 0;
1 1 9 3].
if I use the sortrows function in Matlab B = sortrows(A). I will get this.
B = [1 -9 8 0;
1 1 9 3;
1 3 11 -2;
10 -12 4 8].
The answer I wanted is this.
B = [1 3 11 -2;
1 -9 8 0;
1 1 9 3;
10 -12 4 8]
The thing is, I want to sort my rows here but only based on column 1. if values in column 1 are same, then don't sort them according to column 2(which in genreal this function perform).
I really appreciate, if anyone can help me with this.
Thank you.
matlab sorting
This question already has an answer here:
Sorting entire matrix according to one column in matlab
2 answers
I have this matrix A,
A= [10 -12 4 8;
1 3 11 -2;
1 -9 8 0;
1 1 9 3].
if I use the sortrows function in Matlab B = sortrows(A). I will get this.
B = [1 -9 8 0;
1 1 9 3;
1 3 11 -2;
10 -12 4 8].
The answer I wanted is this.
B = [1 3 11 -2;
1 -9 8 0;
1 1 9 3;
10 -12 4 8]
The thing is, I want to sort my rows here but only based on column 1. if values in column 1 are same, then don't sort them according to column 2(which in genreal this function perform).
I really appreciate, if anyone can help me with this.
Thank you.
This question already has an answer here:
Sorting entire matrix according to one column in matlab
2 answers
matlab sorting
matlab sorting
edited Nov 20 '18 at 15:49
obchardon
3,8221820
3,8221820
asked Nov 20 '18 at 15:34
Ketan SahuKetan Sahu
154
154
marked as duplicate by obchardon, Community♦ Nov 21 '18 at 8:27
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by obchardon, Community♦ Nov 21 '18 at 8:27
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Sort the first column only with the function sort keep only the index, then use this index to sort the whole matrix.
A= [10 -12 4 8;
1 3 11 -2;
1 -9 8 0;
1 1 9 3];
[~,ind] = sort(A(:,1))
B = A(ind,:)
or simply use the second argument of the function sortrows which precise the column sorting vector:
B = sortrows(A,1)
Hello, @Obchardon, Thank you so much, It'sworking, really appreciate your effort. Thank you :)
– Ketan Sahu
Nov 20 '18 at 15:56
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sort the first column only with the function sort keep only the index, then use this index to sort the whole matrix.
A= [10 -12 4 8;
1 3 11 -2;
1 -9 8 0;
1 1 9 3];
[~,ind] = sort(A(:,1))
B = A(ind,:)
or simply use the second argument of the function sortrows which precise the column sorting vector:
B = sortrows(A,1)
Hello, @Obchardon, Thank you so much, It'sworking, really appreciate your effort. Thank you :)
– Ketan Sahu
Nov 20 '18 at 15:56
add a comment |
Sort the first column only with the function sort keep only the index, then use this index to sort the whole matrix.
A= [10 -12 4 8;
1 3 11 -2;
1 -9 8 0;
1 1 9 3];
[~,ind] = sort(A(:,1))
B = A(ind,:)
or simply use the second argument of the function sortrows which precise the column sorting vector:
B = sortrows(A,1)
Hello, @Obchardon, Thank you so much, It'sworking, really appreciate your effort. Thank you :)
– Ketan Sahu
Nov 20 '18 at 15:56
add a comment |
Sort the first column only with the function sort keep only the index, then use this index to sort the whole matrix.
A= [10 -12 4 8;
1 3 11 -2;
1 -9 8 0;
1 1 9 3];
[~,ind] = sort(A(:,1))
B = A(ind,:)
or simply use the second argument of the function sortrows which precise the column sorting vector:
B = sortrows(A,1)
Sort the first column only with the function sort keep only the index, then use this index to sort the whole matrix.
A= [10 -12 4 8;
1 3 11 -2;
1 -9 8 0;
1 1 9 3];
[~,ind] = sort(A(:,1))
B = A(ind,:)
or simply use the second argument of the function sortrows which precise the column sorting vector:
B = sortrows(A,1)
edited Nov 20 '18 at 15:48
answered Nov 20 '18 at 15:43
obchardonobchardon
3,8221820
3,8221820
Hello, @Obchardon, Thank you so much, It'sworking, really appreciate your effort. Thank you :)
– Ketan Sahu
Nov 20 '18 at 15:56
add a comment |
Hello, @Obchardon, Thank you so much, It'sworking, really appreciate your effort. Thank you :)
– Ketan Sahu
Nov 20 '18 at 15:56
Hello, @Obchardon, Thank you so much, It'sworking, really appreciate your effort. Thank you :)
– Ketan Sahu
Nov 20 '18 at 15:56
Hello, @Obchardon, Thank you so much, It'sworking, really appreciate your effort. Thank you :)
– Ketan Sahu
Nov 20 '18 at 15:56
add a comment |