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

12 次查看(过去 30 天)
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.

采纳的回答

Matt
Matt 2014-11-20
编辑:Matt 2014-11-20
Khanh,
Try something like this. . .
ind250 = x(:,1) == 250;
if numel(ind250)>1
x(ind250(2:end),:) = [];
end

更多回答(2 个)

C.J. Harris
C.J. Harris 2014-11-20
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 个评论
Matt
Matt 2014-11-20
Note that the unique will remove all non-unique values, not just the ones with 250 in the first column. Still, if this is the goal, than this is a much better solution than removing elements one by one
Khanh
Khanh 2014-11-23
编辑:Khanh 2014-11-23
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.

请先登录,再进行评论。


Andrew Reibold
Andrew Reibold 2014-11-20
编辑:Andrew Reibold 2014-11-20
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

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by