distance and matrix unique numbers

1 view (last 30 days)
jenka
jenka on 29 Sep 2011
Could you, guys, help me please. I have a matrix X X =
2.2000 3.3000
2.2000 3.3000
4.0000 5.0000
3.0000 5.0000
I need to get from this [index,A]
index = [2] or equivalently index = [1]
and
A =
2.2000 3.3000
4.0000 5.0000
3.0000 5.0000
Then I need to calculate the distance to the nearest neighbor within that matrix A. i.e. it will be 3x1 vector of distances.
Any help please. Thanks

Answers (4)

the cyclist
the cyclist on 29 Sep 2011
I don't really understand the output you want. In the first step, do you just want the unique rows? You can do that with:
>> [uniqueX,i,j] = unique(X,'rows')
Read the help file for the unique() function for more details.
  2 Comments
jenka
jenka on 29 Sep 2011
the cyclist, it works for a simple example above. However, for some reason, it does not work for my bigger problem, i.e. the uniqueX matrix has many rows that are the same (which I want to avoid). Any suggestion?
jenka
jenka on 29 Sep 2011
also, it appears that the first element in UnigueX does not equal the first element in X. Why? Thanks

Sign in to comment.


jenka
jenka on 29 Sep 2011
Hi, well, I tried that but for my data it does not have unique numbers for some reason (maybe it is something to do decimal truncation?) Also, how to then calculate distance vector for matrix uniqueX. Thanks!!!!

Fangjun Jiang
Fangjun Jiang on 29 Sep 2011
Do you mean this:
X=[2.2000 3.3000
2.2000 3.3000
4.0000 5.0000
3.0000 5.0000];
index=2;
A=X;
A(index,:)=[];
D=dist(A')
Function dist() requires Neural Network Toolbox. You may also check the function pdist(), which requires Statistics Toolbox.

Walter Roberson
Walter Roberson on 29 Sep 2011
  2 Comments
jenka
jenka on 29 Sep 2011
however, it does give me the same rows. I checked my calculating the nearest neighbor. The results for these "repeated" rows is 0 which is wrong.
Walter Roberson
Walter Roberson on 29 Sep 2011
Subtract one of the repeated rows from another: the result will very likely include a non-zero result. Make sure you are using
format long g
as format f might truncate the results.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!