简体   繁体   English

在矩阵matlab中找到最接近的值

[英]Find the closest value in a matrix matlab

How can I find the closest element in a matrix in matlab? 如何在matlab中找到矩阵中最接近的元素?

Suppose I have a matrix of the size 300x200 and I want to find the value and the index of the element in the matrix which is the closest to a element given. 假设我有一个大小为300x200的矩阵,我想找到矩阵中与给定元素最接近的元素的值和索引。

Does anyone know how this can be done in matlab? 有谁知道如何在matlab中完成这项工作? I know how to do this for a given array but I can not figure out how this is done for a matrix. 我知道如何为给定的数组执行此操作,但我无法弄清楚如何为矩阵完成此操作。

Let matrix denote your matrix, and ref denote the reference value you want to get closest to. matrix表示您的矩阵,而ref表示您想要最接近的参考值。 Then you can use 然后你可以使用

[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 gives the value of the closest entry; value给出最近条目的值; and row , col give its row and column indices. rowcol给出它的行和列索引。

A smaller case might help you understand - 较小的案例可能有助于您理解 -

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 can be seen, the output matches the expected answer. 可以看出,输出与预期答案匹配。

Extended Part: If you have a set of search numbers, you can process them for closeness very efficiently using bsxfun . 扩展部分:如果您有一组搜索号,则可以使用bsxfun非常有效地处理它们。 This is shown below - 如下所示 -

Code

%%// 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) 

Output 产量

x =
     4     6     4     4


y =
     5     5     4     2

Divakar answer is good, and bsxfun() is a very useful function to learn, but I think in this case it's a bit of an overkill. Divakar的答案很好,而bsxfun()是一个非常有用的学习函数,但我认为在这种情况下它有点过分。 Here you have another way of doing it using linear indexing for the matrix a : 在这里你有另一种方法,使用线性索引为矩阵a

a=rand(3);

a1=a(1,2)+0.001;

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

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

Hope that helps! 希望有所帮助!

An inline function can be created to do this task based on Jana's solution. 可以基于Jana的解决方案创建内联函数来执行此任务。 This solution only works on vectors. 此解决方案仅适用于矢量。

    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);

When combined with Divakars solution an inline function can be created which will perform the requested task. 当与Divakars解决方案结合使用时,可以创建一个内联函数来执行所请求的任务。 The function itself is complicated, but its usage is simple. 函数本身很复杂,但它的用法很简单。

    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)

Even faster can be 甚至更快

a=rand(10,10); 一个= RAND(10,10);

element=a(3,4)+0.00001; 元素=α(3,4)0.00001;

[x,y]=find(abs(a-element)==min(abs(a-element))) [X,Y] =找到(ABS(A-元件)==分钟(ABS(A-元件)))

at least in the case I used it 至少在我使用它的情况下

I was doing more homework assignments from MIT's OCW ( question number 5 ) and this thread helped me a lot! 我在麻省理工学院的OCW( 问题编号5 )做了更多的家庭作业,这个帖子给了我很多帮助! So I came with another solution in which all of you contributed somehow. 所以我带来了另一个解决方案,其中所有人都以某种方式贡献了。

If you want to represent this as a function, it could be written like this. 如果你想把它表示为一个函数,可以像这样写。

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've tried this with row vectors, column vectors and matrices. 我用行向量,列向量和矩阵试过这个。 It works for all of them. 它适用于所有人。 The outcome is n (for rows) and m (for columns). 结果是n(对于行)和m(对于列)。 If there are two or more values that are equally distant, the values of n and m will be larger too. 如果有两个或多个等距离的值,则n和m的值也会更大。 Let's say we have 3 values that are equally close to our reference, our outcome should have n= n1, n2, n3 and m= m1, m2, m3. 假设我们有3个值与我们的参考值相等,我们的结果应该是n = n1,n2,n3和m = m1,m2,m3。 Where the position of every value is (n1, m1), (n2, m2) and (n3, m3). 每个值的位置是(n1,m1),(n2,m2)和(n3,m3)。

An example of it's use: 它的一个例子是:

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

The outcome is: 结果是:

a =

     1
     2
     3
     4


b =

     1
     2
     3
     4

Hope this helps a bit :) 希望这个对你有帮助 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM