繁体   English   中英

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

[英]Find the closest value in a matrix matlab

如何在matlab中找到矩阵中最接近的元素?

假设我有一个大小为300x200的矩阵,我想找到矩阵中与给定元素最接近的元素的值和索引。

有谁知道如何在matlab中完成这项工作? 我知道如何为给定的数组执行此操作,但我无法弄清楚如何为矩阵完成此操作。

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给出最近条目的值; rowcol给出它的行和列索引。

较小的案例可能有助于您理解 -

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

产量

x =
     4

y =  
     5

可以看出,输出与预期答案匹配。

扩展部分:如果您有一组搜索号,则可以使用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

Divakar的答案很好,而bsxfun()是一个非常有用的学习函数,但我认为在这种情况下它有点过分。 在这里你有另一种方法,使用线性索引为矩阵a

a=rand(3);

a1=a(1,2)+0.001;

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

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

希望有所帮助!

可以基于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)

甚至更快

一个= RAND(10,10);

元素=α(3,4)0.00001;

[X,Y] =找到(ABS(A-元件)==分钟(ABS(A-元件)))

至少在我使用它的情况下

我在麻省理工学院的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 

我用行向量,列向量和矩阵试过这个。 它适用于所有人。 结果是n(对于行)和m(对于列)。 如果有两个或多个等距离的值,则n和m的值也会更大。 假设我们有3个值与我们的参考值相等,我们的结果应该是n = n1,n2,n3和m = m1,m2,m3。 每个值的位置是(n1,m1),(n2,m2)和(n3,m3)。

它的一个例子是:

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

结果是:

a =

     1
     2
     3
     4


b =

     1
     2
     3
     4

希望这个对你有帮助 :)

暂无
暂无

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

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