MATLAB Answers

## Find unique in matrix of x,y coordinates

Asked by Ruchi Bhatia

### Ruchi Bhatia (view profile)

on 19 Dec 2017
Latest activity Commented on by Ruchi Bhatia

### Ruchi Bhatia (view profile)

on 20 Dec 2017
Accepted Answer by Kelly Kearney

### Kelly Kearney (view profile)

Hello, I would love your help.
If I have a matrix of x,y coordinates for example:
x y
A=[ 2.5 3.5
1.2 3.5
2.2 1.2
1.2 3.5
4.0 2.2 ]
How do I find a unique of this? In the above example one instance of x,y (1.2, 3.5) will be removed?

#### 0 Comments

Sign in to comment.

## 3 Answers

### Kelly Kearney (view profile)

Answer by Kelly Kearney

### Kelly Kearney (view profile)

on 19 Dec 2017
Accepted Answer

To find unique rows, you can use unique with the 'rows' option. (The 'stable' option preserves the input order, if that's important for your application).
unique(A, 'rows', 'stable')

Ruchi Bhatia

### Ruchi Bhatia (view profile)

on 19 Dec 2017
Thanks. Wow, didn't realize it was that straight forward.

Sign in to comment.

### Image Analyst (view profile)

Answer by Image Analyst

### Image Analyst (view profile)

on 19 Dec 2017

Try unique() with the 'rows' option:
uniqueRowsOfA = unique(A, 'rows')

Ruchi Bhatia

### Ruchi Bhatia (view profile)

on 20 Dec 2017
thank you

Sign in to comment.

### Kaushik Lakshminarasimhan (view profile)

Answer by Kaushik Lakshminarasimhan

### Kaushik Lakshminarasimhan (view profile)

on 19 Dec 2017

Did you try this?
[C,IA] = unique(A,'rows');
C contains all unique rows of A. IA contains the indices of the rows of A that are in C. So if you want the indices of the rows that were removed, you can use:
[nrows,~] = size(A);
notIA = setdiff(1:nrows,IA); % duplicate rows

Ruchi Bhatia

### Ruchi Bhatia (view profile)

on 19 Dec 2017
Thank you! Very helpful.

Sign in to comment.