Find the closest value in the matrix matrix

How to find the closest element in a matrix in matlab?

Suppose I have a size matrix 300x200, and I want to find the value and index of the element in the matrix that is closest to the specified element.

Does anyone know how to do this in matlab? I know how to do this for a given array, but I cannot figure out how to do this for a matrix.

+4
source share
6 answers

A small case can help you understand -

the code

%%// Given big matrix, taken as a matrix of random numbers for demo
a1 = rand(10,5); %%// Replace this with your 300X200 matrix

%// For demo, let us assume, you are looking for the element that happens to be closest to the element in the 4th row and 5th column, which will be verified at the end 
element = a1(4,5)+0.00001; %%// The element in search

%%// Find the linear index of the location
[~,ind] = min(reshape(abs(bsxfun(@minus,a1,element)),numel(a1),[]));

%%// Convert the linear index into row and column numbers
[x,y] = ind2sub(size(a1),ind) 

Output

x =
     4

y =  
     5

As you can see, the result corresponds to the expected answer.

:. , , bsxfun. -

%%// Given big matrix, taken as a matrix of random numbers for demo
a1 = rand(10,5); %%// Replace this with your 300X200 matrix

%// For an array of search numbers
search_array = [a1(4,5)+0.00001;a1(6,5)+0.00001;a1(4,4)+0.00001;a1(4,2)+0.00001];

%%// Find the linear index of the location
[~,ind] = min(abs(bsxfun(@minus,a1(:),search_array')));%//'

%%// Convert the linear index into row and column numbers
[x,y] = ind2sub(size(a1),ind) 

x =
     4     6     4     4


y =
     5     5     4     2
+3

matrix , ref , .

[value, ii] = min(abs(matrix(:)-ref));    %// linear index of closest entry
[row, col] = ind2sub(size(matrix), ii);   %// convert linear index to row and col

value ; row, col .

+10

Divakar , bsxfun() - , , . , a:

a=rand(3);

a1=a(1,2)+0.001;

[~,ind]=min(abs(a(:)-a1));

[x,y]=ind2sub(3,ind);

, !

+3

Jana. .

    nearest_index = @(vector,element) find(abs(element-vector) == min(abs(element-vector)));

    vector = [9 8 7 6 5 4 3 2 1];
    element = 3.1;
    index = nearest_index(vector,element);
    value = vector(index);

Divakars , . , .

    nearest_index = @(matrix,element) find(abs( ...
        repmat(element,size(matrix)) - matrix) == ...
        min(reshape(abs(repmat(element,size(matrix)) - matrix), ...
        [1,numel(abs(repmat(element,size(matrix)) - matrix))])));

    matrix = rand(10,5); 
    element = matrix(4,5)+0.00001;
    [x, y] = nearest_index (matrix,element) 
    value = matrix(x,y)
+1

= Rand (10,10);

= (3,4) 0,00001;

[, ] = ( (-) == ( (-)))

,

0

MIT OCW ( № 5), ! , .

, .

function [n, m]=findNearest(x, y)
theAbs=abs((x(:))-y); % Calculates absolute value of the difference
minValues=find(theAbs==min(theAbs)); % finds the position where one or more numbers match the criteria
[n, m]=ind2sub(size(x), minValues); % Returns one or multiples values and their indices, if the distance between them is the same.
return 

I tried this with row vectors, column vectors and matrices. It works for everyone. The result is n (for rows) and m (for columns). If there are two or more values ​​that are equally distant, the values ​​of n and m will also be larger. Let them say that we have 3 values ​​that are equally close to our link, our result should have n = n1, n2, n3 and m = m1, m2, m3. Where is the position of each value (n1, m1), (n2, m2) and (n3, m3).

Usage example:

x=eye(4,4);
y=1.698;
[a, b]=findNearest(x, y)

Result:

a =

     1
     2
     3
     4


b =

     1
     2
     3
     4

Hope this helps a bit :)

0
source

Source: https://habr.com/ru/post/1533155/


All Articles