Finding rows with the closest values in two matrices matlab

조회 수: 13 (최근 30일)
Jiali
Jiali 2015년 9월 24일
댓글: Jiali 2015년 9월 26일
I have two matrices with the same size [n,2]. One is the control matrix A and another is the test matrix B. In general, Numbers in B have close values to numbers in A. However, the each matched numbers in B is randomly distributed. The matched index need to be found to sort matrix B in the same sequence as A. How can I achieve it efficiently? For example:
A=
[-0.9 235.6
0.0 0.0
-0.2 76.8
-0.4 153.5
-0.7 312.7
-0.5 389.4
-0.6 466.6
0.1 548.7
0.1 625.5
0.7 702.6
0.8 779.3
1.4 861.1
2.2 938.0
3.2 1014.4];
B=
[
-1.037 312.275
-0.839 235.292
-0.826 389.108
-1.071 466.333
-0.542 548.430
-0.635 625.385
0.000 0.000
-0.210 76.612
-0.386 153.425
-0.302 702.214
0.147 779.087
0.522 860.808
1.355 937.624
1.928 1014.046];
I know I can use loop to solve this issue, but my matrix is quite big, the loop will be time-consuming. I also know that knnsearch can solve this problem, however, I can not use the statistical tool box. Does anyone have other good suggestions? Highly appreciate.
  댓글 수: 1
James Tursa
James Tursa 2015년 9월 24일
편집: James Tursa 2015년 9월 24일
What are the actual sizes of your matrices? I.e., how big is "big"? Can you sort A and B individually, then compare the results and tweak them (if necessary) to get the optimum match?

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

채택된 답변

Jiali
Jiali 2015년 9월 24일
this method doesn't work because all the numeric data are generated from some images. Since the images are not always same/aligned, the data can not be simply sorted in matrix A and B to match each other.
  댓글 수: 2
dpb
dpb 2015년 9월 24일
편집: dpb 2015년 9월 25일
Might consider pdist2 -- if the values are unique-enough, one application will provide the needed result. If not, iterate thru after selecting however many are in first result, eliminating those from the distance calculation in next iteration.
MIGHT be more efficient if there are a sizable fraction eliminated in first pass; otherwise the stepwise computation of nearest neighbor in a loop is likely as good as it gets. That really shouldn't take long if you preallocate the result.
Jiali
Jiali 2015년 9월 26일
Thank you a lot. However, I found that my version of Matlab doesn't have pdist2. So sadly.

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

추가 답변 (4개)

dpb
dpb 2015년 9월 24일
Well, B is sorted on column one in ascending order; if they're the same size vectors the closest you can get globally is to do the same thing for A...even if you can make one or two closer to the alternate entry by swapping them around, doing so will simply make the other discrepancies larger as there's nothing to do with them but stick 'em in somewhere...
A=sortrows(A,1);

James Tursa
James Tursa 2015년 9월 24일
편집: James Tursa 2015년 9월 24일
E.g., not sure if this is what you want but to make B look like the order in A, keying off of the 2nd column and keeping the rows the same, you could do something like this:
[~,ia] = sort(A(:,2));
[~,ib] = sort(B(:,2));
[~,ix] = sort(ia);
Bnew = B(ib(ix),:);
This assumes that the values are far enough apart so that the sort works. Otherwise tweaking after the fact would be needed. Also assumes the rows need to be kept intact, but if not you could do each column separately.

Andrei Bobrov
Andrei Bobrov 2015년 9월 25일
[ma, na] = size(A);
[ia,ja] = ndgrid(0:ma-1,0:na-1);
F = griddedInterpolant(ia,ja,A);
[mb, nb] = size(B);
[ib,jb] = ndgrid(linspace(0,ma-1,mb),linspace(0,na-1,nb));
AszB = F(ib,jb);
[~,iaa] = sort(AszB(:));
[~,ibb] = sort(B(:));
[~,siaa] = sort(iaa);
out = reshape(B(ibb(siaa)),[mb, nb]);

Jiali
Jiali 2015년 9월 26일
I find one useful code in the file exchange called Inter-Point Distance Matrix. http://www.mathworks.com/matlabcentral/fileexchange/18937-ipdm--inter-point-distance-matrix

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

제품

Community Treasure Hunt

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

Start Hunting!

Translated by