簡體   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