Which function should I use here to group my matrix?

Hello community,
Just an hour before, I asked a question, but I find out I was wrong with my interpretation and I apologise for it.
I like to ask again the modified question. I appreciate ,if you people could help me.
Here is my question.
if I have this matrix.
A = [1 0 0;
-2 0 0;
1.1 0 0;
-2.1 0 0;
1.2 0 0;
-2.2 0 0]
if i use here the sort function, I will get this
B = sort(A)
B = [-2.2 0 0;
-2.1 0 0;
2 0 0;
1 0 0;
1.1 0 0;
1.2 0 0].
and the answer I want is this,
B = [1 0 0;
1.1 0 0;
1.2 0 0;
-2 0 0;
-2.1 0 0;
-2.2 0 0].
What function should I use here? I believe this is something related to group or sorting function.
Thank you,

 Accepted Answer

[~,ii] = sort(abs(A(:,1)));
B = A(ii,:);

3 Comments

Hello, Andrei, Really appreciate your answer. Thank you.
This is working perfectly with my code. Eventually, I got a new matrix again I have to do grouping. Could you please help me wwith this also.
My new matrix is this.
Axis_A = [0.0045 0.78;
-0.0015 0.78;
0.0016 0.78;
-0.0046 0.78;
0.0045 0.78;
-0.0015 0.78;
0.0016 0.78;
-0.0046 0.78;
0.0045 0.78;
-0.0015 0.78;
0.0016 0.78;
-0.0046 0.78].
if you see here in column 1 my values of row 1,5 and 9 are exactly same, values of row 2,6 and 10 are same, values of row 3,7 and 11 are same and values of row 4,8 and 12 are same. In this case, I want to group/sort them in the following order.
Axis_B= [0.0045 0.78;
0.0045 0.78;
0.0045 0.78;
-0.0015 0.78;
-0.0015 0.78;
-0.0015 0.78;
0.0016 0.78;
0.0016 0.78;
0.0016 0.78;
-0.0046 0.78;
-0.0046 0.78;
-0.0046 0.78].
I try to play with your code in this case also, but didnt get the above answer.
I again would like to thank you for providing me answer for the previous question.
~ketan :)
Axis_A = [0.0045 0.78;
-0.0015 0.78;
0.0016 0.78;
-0.0046 0.78;
0.0045 0.78;
-0.0015 0.78;
0.0016 0.78;
-0.0046 0.78;
0.0045 0.78;
-0.0015 0.78;
0.0016 0.78;
-0.0046 0.78];
[~,~,c] = unique(Axis_A(:,1),'stable');
[~,ii] = sort(c);
Axis_B = Axis_A(ii,:);
Thank you, Andrei Bobrov.
That works. I honestly appreciate your answer and efforts. Thanks you so much.
Kind regards,
Ketan

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements 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!