필터 지우기
필터 지우기

distance and matrix unique numbers

조회 수: 2 (최근 30일)
jenka
jenka 2011년 9월 29일
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

답변 (4개)

the cyclist
the cyclist 2011년 9월 29일
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
jenka
jenka 2011년 9월 29일
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 2011년 9월 29일
also, it appears that the first element in UnigueX does not equal the first element in X. Why? Thanks

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


jenka
jenka 2011년 9월 29일
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 2011년 9월 29일
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 2011년 9월 29일
  댓글 수: 2
jenka
jenka 2011년 9월 29일
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 2011년 9월 29일
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.

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

Community Treasure Hunt

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

Start Hunting!

Translated by