在matlab中找到矩阵中具有相同值的最近元素

在matlab中找到矩阵中具有相同值的最近元素,matlab,matrix,closest,Matlab,Matrix,Closest,考虑以下矩阵: 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”,我希望获得最接近的“6”的行和列值,最近的是第4行、第5列和第4行、第6列,它们中的任何一个都可以。我已经查找了“bwdist”和“find”函数,但它们没有给出正确的结果。有

考虑以下矩阵:

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”,我希望获得最接近的“6”的行和列值,最近的是第4行、第5列和第4行、第6列,它们中的任何一个都可以。我已经查找了“bwdist”和“find”函数,但它们没有给出正确的结果。有人能帮我吗

编辑:

“min”函数在此不起作用,因为存在所需元素的每个位置都将被转换为零,“min”逐行扫描矩阵并给出第一个零的位置。这种情况如下:

 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
即使第4行第5列的“5”旁边有一个“5”,第10行第2列的“5”也会被选中。

假设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)

这种方法不需要任何工具箱。如果不存在具有相同值的其他条目,则返回[]

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

听着,我想这很好地回答了你的问题:对不起,不管我搜索了多少,我都找不到这个!谢谢!!当然没问题!如果这回答了你的问题,我想你可以删除它,因为它将是重复的。谢谢,祝你好运!我浏览了链接,但它对整数不起作用。我已编辑了我的问题,以包含我从链接中获取的代码。谢谢:你能告诉我在没有匹配项的情况下如何检查大小写吗?@Matt So,最接近的值?我想你需要一些加权标准来使用距离的接近度和它们的值的接近度?你能解释一下这在技术上的作用吗:mindistmat:?是否有类似于distmat:的东西用于行?@Matt Well min with distmat:基本上是在行和列之间搜索最小值。你想限制只在同一行中找到最近的一个吗?这也很好,我自己也在考虑给bwdist提供一些替代方案,但后来我想让我们为你跳过它@Divakar:就是这样:-@Luis Mendo非常感谢:它真的很有用。
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);