Matlab Matrix: Eliminating Duplicate Entries

Hello, all, thanks for reading this post.
I have a problem with a point coordinate matrix I create. I output the matrix correctly, but one of the bugs in the program I inherited is every point after point 1 is duplicated. I looks something like:
0 0 0
0 0 3.0000
0 0 3.0000
0 1.4225 4.8659
0 1.4225 4.8659
0 -1.4269 4.8520
0 -1.4269 4.8520
1.1125 2.3073 6.0264
1.1125 2.3073 6.0264
-1.1160 2.3007 6.0177
-1.1160 2.3007 6.0177
1.1086 -2.3144 6.0039
1.1086 -2.3144 6.0039
-1.1120 -2.3078 5.9953
-1.1120 -2.3078 5.9953
for 8 coordinates (and 7 connections, as this is a binary tree).
Is there a way to output a new point coordinate matrix where I have 8 rows, and no duplicate points?
Thanks
Edit: unique(A,'rows') will work in my case (because the point coordinates are exactly the same), but when I use it it outputs the coordinates in alpha-numeric order. Is there a way to output the coordinates in their original order, minus the duplicates?

 채택된 답변

Fangjun Jiang
Fangjun Jiang 2011년 11월 17일

0 개 추천

unique(A,'rows')

댓글 수: 6

Walter Roberson
Walter Roberson 2011년 11월 17일
Note: if the above does not work, then the duplicates might not be *exact* duplicates -- e.g., the difference between them might be on the order of 1E-15 . If that is the case, you might need a different strategy.
Brian
Brian 2011년 11월 17일
Actually, unique(A,'rows') will work in my case (because the point coordinates are exactly the same), but when I use it it outputs the coordinates in alpha-numeric order. Is there a way to output the coordinates in their original order, minus the duplicates?
Fangjun Jiang
Fangjun Jiang 2011년 11월 17일
Yes. the output of unique() is sorted. You can do this to get the original order.
a=[9 8 9 7 6 7 5 4 8];
[b,ind]=unique(a,'first');
b=a(sort(ind));
Brian
Brian 2011년 11월 17일
Thanks, I tried that but when I did it it only sorted the first column and deleted the other three (making it a 1 by 8 array, effectively deleting the y and z coordinates).
What I mean is: it turned:
0 0 0
0 0 3.0000
0 0 3.0000
0 1.4225 4.8659
0 1.4225 4.8659
0 -1.4269 4.8520
0 -1.4269 4.8520
1.1125 2.3073 6.0264
1.1125 2.3073 6.0264
-1.1160 2.3007 6.0177
-1.1160 2.3007 6.0177
1.1086 -2.3144 6.0039
1.1086 -2.3144 6.0039
-1.1120 -2.3078 5.9953
-1.1120 -2.3078 5.9953
into:
0
0
0
0
1.1125
-1.1160
1.1086
-1.1120
Which is right for the x coordinates. Is it possible to extend the sort to the entire matrix, not just the first column?
Walter Roberson
Walter Roberson 2011년 11월 17일
Using the indexing I suggested in my answer would deal with these issues much more readily...
Fangjun Jiang
Fangjun Jiang 2011년 11월 17일
It's a matter of selecting all the columns.
b=a(sort(ind),:)

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2011년 11월 17일

1 개 추천

A(1:2:end,:)
This does not have the problem with comparing nearly equal quantities, but it does assume that the second row of each pair is acceptable

댓글 수: 1

Brian
Brian 2011년 11월 17일
Thanks, this worked exactly as I wanted to! Sorry I didn't see your comment until now, but this worked exactly as I needed it to.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

질문:

2011년 11월 17일

Community Treasure Hunt

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

Start Hunting!

Translated by