# how to find nearest values of all elements of a matrix to another matrix in matlab

18 Ansichten (letzte 30 Tage)
LUI PAUL am 29 Mär. 2015
Beantwortet: daniel mitchell am 2 Jan. 2022
hi all, i have 2 matrices of different size(11527*1 and 112813*1) .i want to find the nearest values(distance between the two element <= some condition(say <=2)) of each element of matrix 1 (11527*1) with respect to matrix 2 (112813*1). The result should be of the size of matrix 1. Can anyone help me? one corresponding figure is given.
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Akzeptierte Antwort

Jos (10584) am 29 Mär. 2015
A = [1 5 7 3 2 8]
B = [4 12 11 10 9 23 1 15]
TMP = bsxfun(@(x,y) abs(x-y), A(:), reshape(B,1,[]))
[D, idxB] = min(TMP,[],2)
Result = B(idxB)
TFDiffLessThen3 = D < 3 % different outcome than in your example??
##### 2 KommentareKeine anzeigenKeine ausblenden
LUI PAUL am 30 Mär. 2015
Jos (10584) am 31 Mär. 2015

You are free to post questions to this forum :-)

Melden Sie sich an, um zu kommentieren.

### Weitere Antworten (3)

Jos (10584) am 30 Mär. 2015
Take a look at NEARESTPOINT as this function does exactly what you want and is pretty damn fast, if I say so myself :-)
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

Roger Stafford am 29 Mär. 2015
With vectors as large as these it could very well be advisable to sort the second vector. Let A be the first 11527 x 1 vector and B the second 112813 x 1 vector.
T = sort(B);
[~,ix] = histc(A,[-inf;(T(1:end-1)+T(2:end))/2;inf]);
Result = T(ix);
Diff3 = abs(Result-A)<3;
##### 4 Kommentare2 ältere Kommentare anzeigen2 ältere Kommentare ausblenden
Roger Stafford am 30 Mär. 2015
@Lui: "Thanks Roger for answer but it is showing Error using ==> vertcat CAT arguments dimensions are not consistent."
This probably means that you have used a row vector for your second vector in spite of stating that its dimensions are 112813 x 1. Change the first line of my code to:
T = sort(B(:);
and the last line to:
Diff3 = abs(Result-A(:))<3;
@Jos: As Lui has stated this problem it doesn't matter if multiple copies of the same value occur in that second vector. Only the values are listed in 'Result', not their locations. Therefore it should work even if the elements are not unique.
Jos (10584) am 31 Mär. 2015
@Roger. True! Apparently, histc does now accept also non-strictly increasing edges ...

Melden Sie sich an, um zu kommentieren.

daniel mitchell am 2 Jan. 2022
I think this may help:
A = [1 5 7 3 2 8]';
B = [4 12 11 10 9 23 1 15]';
I = knnsearch(B,A);
R = B(I) % Result
R = 6×1
1 4 9 4 1 9
D = abs(A-R)
D = 6×1
0 1 2 1 1 1
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by