## Finding rows with the closest values in two matrices matlab

### Jiali (view profile)

on 24 Sep 2015
Latest activity Commented on by Jiali

### Jiali (view profile)

on 26 Sep 2015
Accepted Answer by Jiali

### Jiali (view profile)

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.

James Tursa

### James Tursa (view profile)

on 24 Sep 2015
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?

### Tags

No tags entered yet.

### Jiali (view profile)

on 24 Sep 2015

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.

dpb

### dpb (view profile)

on 24 Sep 2015
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 (view profile)

on 26 Sep 2015
Thank you a lot. However, I found that my version of Matlab doesn't have pdist2. So sadly.

### dpb (view profile)

on 24 Sep 2015

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 (view profile)

Answer by James Tursa

### James Tursa (view profile)

on 24 Sep 2015
Edited by James Tursa

### James Tursa (view profile)

on 24 Sep 2015

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 (view profile)

Answer by Andrei Bobrov

### Andrei Bobrov (view profile)

on 25 Sep 2015

[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 (view profile)

on 26 Sep 2015
I will try your code. Appreciate your example.