# Delete rows that have the first same value and keep one row in matrix with 2 colums?

44 views (last 30 days)
Khanh on 20 Nov 2014
Edited: Khanh on 23 Nov 2014
Hi everyone,
I have an array:
x=[0,0 ; 250,1050 ; 250,1051 ; 1173,3050 ; 1173 3150].
I don't know how to delete the rows that has the first value is 250 and just keep one.
The row may be (250,1050) or (250,1051), whatever but just one row.
Could someone please suggest me how to do it?
Many thanks.
##### 2 CommentsShowHide 1 older comment
Khanh on 20 Nov 2014
Thanks for your reply. Just value 250

Sign in to comment.

### Accepted Answer

Matt on 20 Nov 2014
Edited: Matt on 20 Nov 2014
Khanh,
Try something like this. . .
ind250 = x(:,1) == 250;
if numel(ind250)>1
x(ind250(2:end),:) = [];
end
##### 2 CommentsShowHide 1 older comment
Khanh on 21 Nov 2014
Edited: Khanh on 21 Nov 2014
Thanks all of you. Matt'answer is the best I want. It works great.

Sign in to comment.

### More Answers (2)

C.J. Harris on 20 Nov 2014
Do you mean something like this?
x = [0,0 ; 250,1050 ; 250,1051 ; 1173,3050 ; 1173 3150];
[~,idx] = unique(x(:,1));
out = x(idx,:)
out =
0 0
250 1051
1173 3150
##### 2 CommentsShowHide 1 older comment
Khanh on 23 Nov 2014
Edited: Khanh on 23 Nov 2014
The result from this code is
out = [0 0;250 1051;1173 3150]
Can I ask you a question? How can I get the result with 1051 (not 1050 )?
Thanks.

Sign in to comment.

Andrew Reibold on 20 Nov 2014
Edited: Andrew Reibold on 20 Nov 2014
This removes any rows where the value in the first column has already been used.
[~,idx] = unique(x(:,1)); %which rows have a unique first value?
x = x(idx,:) %only use those
Output:
x =
0 0
250 1050
1173 3050
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Categories

Find more on Matrix Indexing 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!