2014-03-24 215 views
4

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

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

有没有人知道这是如何在matlab中完成的?我知道如何为给定的数组做这件事,但我无法弄清楚这是如何为矩阵完成的。

+0

你可以发布你的代码将使用上做到这一点数组?为矩阵做它的方式几乎相同;) – McMa

回答

3

较小的情况下,可能会帮助你理解 -

代码

%%// 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 
+0

是的,这帮助我完全理解这一点,谢谢:) – user3366536

+0

干杯!看看路易斯的做法吧! :) – Divakar

+0

@ user3366536查看编辑后的“扩展部分”,了解如何处理一组搜索数字。 – Divakar

10

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给出它的行和列索引。

+0

+1我的'bsxfun'和'reshape'是过度杀伤! :) – Divakar

+0

你用代码和评论打败了我,只用了一秒钟:D – McMa

3

Divakar答案很好,bsxfun()是一个非常有用的功能学习,但我认为在这种情况下,它有点矫枉过正。在这里,你可以选择使用线性索引对矩阵a做的另一种方式:

a=rand(3); 

a1=a(1,2)+0.001; 

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

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

希望帮助!

0

即使更快可以是

一个=兰特(10,10);

element = a(3,4)+0.00001;

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

至少在I的情况下用它

1

内联函数可以创建基于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) 
0

我是做从麻省理工学院的开放式课程更多家庭作业(question number 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 

希望这有助于一点:)

相关问题