# How to remove the swap vectors from a matrix?

1 view (last 30 days)
BALAKRISHNA ADHIKARI on 5 Oct 2020
Hello all,
I am facing few problem in MATLAB code and hoping to get solution from here. I am having a matrix of (30,2). As following
1 5
2 3
2 5
2 24
3 2
3 25
3 44
4 9
5 2
5 1
5 6
6 5
6 7
6 9
7 6
7 25
7 28
8 13
9 6
9 4
9 10
10 9
10 11
10 13
11 10
11 28
11 32
12 17
13 10
13 8
It can be seen that it has [2 3] in second row and [3 2] in fifth row andsimilarly few more rows are there. So, my problem is to remove the swaped row from the original and make another matrix. That means [2 3] will remain in the original matrix and [3 2] will goes to a new matrix. Similarly for other elements. Please help to get the code.
Best Regards

Ameer Hamza on 5 Oct 2020
Try this
[~, idx] = unique(sort(M, 2), 'rows');
M_unique = M(idx, :);
BALAKRISHNA ADHIKARI on 5 Oct 2020
Thanks Ameer. It did my work.

madhan ravi on 5 Oct 2020
[Original_matrix, c, ~] = unique(sort(matrix, 2), 'rows');
New_matrix = matrix(setdiff(1:size(matrix, 1), c), :)
BALAKRISHNA ADHIKARI on 5 Oct 2020
Thanks ravi.

### Categories

Find more on Creating and Concatenating Matrices 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!

Translated by