Delete rows that have the first same value and keep one row in matrix with 2 colums?
조회 수: 6 (최근 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.
댓글 수: 2
Andrew Reibold
2014년 11월 20일
Do you ONLY want the duplicated 250's removed, or ALL of the duplicated values removed (Like the duped 1173 as well)?
채택된 답변
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
Andrew Reibold
2014년 11월 20일
I think she needs it to work for all duplicates (like 1173 also), not just 250
추가 답변 (2개)
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
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
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
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!