2014-03-24 216 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 

希望這有助於一點:)

相關問題