2015-02-07 22 views
2

查找與矩陣相同的值最接近的元素考慮下面的矩陣:在MATLAB

0 3 0 1 1 4 
1 3 5 6 7 0 
2 5 6 2 6 1 
4 4 2 1 5 1 

當我指定一個元素的位置,我想獲得具有相同值最接近的元素的位置。例如,如果我選擇第3行第3列中的元素,即'6',我希望獲得最接近'6'的行和列值,在這種情況下,它位於第2行第4列。同樣,對於第4行第4列中的'1',最近的是在第4行,第5行和第4行第6列,其中任何一個都很好。我查了'bwdist'和'找到'的功能,但他們沒有給出正確的結果。任何人都可以幫助我解決這個問題?

編輯:

a1 = randi(10,10,5); 
disp(a1); 
%// For an array of search numbers 
search_array = a1(4,5); 
disp(search_array); 
%%// 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) 

「最低」功能將不會在這裏工作爲其中所需的元素存在將被轉換到零,並且每個位置「分鐘」掃描矩陣行方向,並給出第一zero.The以下的位置是這樣的情況下:

2  6 10  9  2 
6  6  7  5  3 
1  8  5  2  1 
8  1  9  5  5 
9  7  6  4  3 
10  6  6  5  3 
10  2  7  9  5 
6 10  4  5  2 
3  6  3  4  5 
2  5  6  4  8 

即使有一個「5」旁邊的「5」排在4,第5欄,「5」在第10行,選擇第2列。

+1

看[這裏](http://stackoverflow.com/questions/22609192/find-the-closest-value-in-a-matrix-matlab)我覺得這回答非常好您的問題:) – 2015-02-07 04:59:58

+0

對不起,無論我搜索多少,我都找不到這個!非常感謝!! – Matte 2015-02-07 05:09:45

+0

肯定沒問題!如果這回答你的問題,我認爲你可以刪除它,因爲它會重複。謝謝,祝你好運! – 2015-02-07 05:10:25

回答

2

假設A爲輸入2D矩陣,這可能是一種方法 -

%// Row and column indices of the "pivot" 
row_id = 4; 
col_id = 5; 

%// Get the linear index from row and column indices 
lin_idx = sub2ind(size(A),row_id,col_id) 

%// Logical array with ones at places with same values 
search_matches = false(size(A)); 
search_matches(A==A(lin_idx)) = 1; 

%// Create a logical array with just a single 1 at the "pivot" 
A_pivot = false(size(A)); 
A_pivot(lin_idx) = 1; 

%// Use BWDIST to find out the distances from the pivot to all the places 
%// in the 2D matrix. Set the pivot place and places with non-similar 
%// values as Inf, so that later on MIN could be used to find the nearest 
%// same values location 
distmat = bwdist(A_pivot) 
distmat(lin_idx) = Inf 
distmat(~search_matches)=Inf 

[~,min_lin_idx] = min(distmat(:)) 
[closest_row_idx,closest_col_idx] = ind2sub(size(A),min_lin_idx) 
+0

謝謝:)你能告訴我如何檢查沒有匹配的情況嗎? – Matte 2015-02-07 07:23:25

+0

@Matt所以,在價值方面最接近?我想你需要一些「權重標準」來同時使用「距離的接近度」和「值的接近度」? – Divakar 2015-02-07 07:24:39

+0

對不起,我明白了:) – Matte 2015-02-07 07:28:37

2

這種方法不需要任何工具箱。如果不存在具有相同值的其他條目,則返回[]

A = [0 3 0 1 1 4 
    1 3 5 6 7 0 
    2 5 6 2 6 1 
    4 4 2 1 5 1];      %// data matrix 
pos_row = 3;       %// row of reference element 
pos_col = 3;       %// col of reference element 

ref = A(pos_row,pos_col);    %// take note of value 
A(pos_row,pos_col) = NaN;    %// remove it, to avoid finding it as closest 
[ii, jj] = find(A==ref);    %// find all entries with the same value 
A(pos_row,pos_col) = ref;    %// restore value 
d = (ii-pos_row).^2+ (jj-pos_col).^2; %// compute distances 
[~, ind] = min(d);      %// find arg min of distances 
result_row = ii(ind);     %// index with that to obtain result 
result_col = jj(ind); 
+0

這也不錯,我在考慮給自己一些替代'bwdist'的選擇,但是後來我覺得讓我們跳過吧;) – Divakar 2015-02-07 16:11:29

+0

@Divakar所以這裏是:-) – 2015-02-07 16:33:29

+0

@路易斯門多非常感謝:)它真的很有用。 – Matte 2015-02-08 05:23:39