MATLAB Answers

How to order a matrix?

16 views (last 30 days)
Sky Scrapper
Sky Scrapper on 23 Apr 2019
Commented: Sky Scrapper on 23 Apr 2019
Hello,
I have a matrix,
A=[ 100 5 0 ; 200 0 -5 ; 300 0 0 ; 300 5 0 ; -100 0 5 ; 300 0 0; 300 0 5];
I want to get,
A= [100 5 0; 200 0 -5 ; 300 0 0; 300 5 0 ; 300 0 5 ; -100 0 5];
I am writing code:
B= unique(A(:, 2:3),'rows');
then it's removing the first row as 5 0 is also repeted for the 4th row 300 5 0 but I need to keep the 1st row.
Is it possible to do?
  4 Comments
Sky Scrapper
Sky Scrapper on 23 Apr 2019
I could solve my problem. thanks for helping me!

Sign in to comment.

Answers (2)

Alex Mcaulley
Alex Mcaulley on 23 Apr 2019
Edited: Alex Mcaulley on 23 Apr 2019
Try this:
[~,ia,~] = unique(A(:, 2:3),'rows','stable');
B = A(ia,:);
  7 Comments
Sky Scrapper
Sky Scrapper on 23 Apr 2019
I have already done this. Again thanks a lot!

Sign in to comment.


Guillaume
Guillaume on 23 Apr 2019
how can i do that?
Use uniquetol instead of unique. Note that the 'rows' option of unique is 'ByRows', true with uniquetol
[~, irows] = uniquetol(A(:, [2 3]), 'ByRows', true); %if you are happy with the default tolerance
B = A(irows, :);
  1 Comment
Sky Scrapper
Sky Scrapper on 23 Apr 2019
Yes, i have already done that.
I had to take tolerance=0.02
Thank you very much for your kind support.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by