简体   繁体   中英

Find closest element with same value in matrix in matlab

Consider the following matrix:

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

When I specify the position of an element, I would like to obtain the position of the nearest element with the same value.For example, if I select the element in row 3,column 3, ie '6', I would like to obtain the the row and column values of the nearest '6',in this case, it is at row 2, column 4.And similarly, for the '1' at row 4,column 4, the nearest is at row 4,col 5 and row 4,col 6, any one of them is are fine.I have looked up the 'bwdist' and 'find' functions but they don't give the proper result.Can anyone help me on this?

Edit:

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) 

The 'min' function will not work here as each position where the required element is present will be converted to zero and 'min' scans the matrix row-wise and gives the position of the first zero.The following is such a case:

 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

Even though there is a '5' right next to the '5' in row 4,col 5,the '5' in row 10, column 2 is selected.

Assuming A to be the input 2D matrix, this could be one approach -

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

This approach doesn't require any toolbox. It returns [] if no other entry with the same value exists.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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