Matlab-为每个零矩阵元素找到最近的非零元素的索引

Matlab-为每个零矩阵元素找到最近的非零元素的索引,matlab,matrix,nearest-neighbor,Matlab,Matrix,Nearest Neighbor,在Matlab中,我有一个矩阵,其中一些元素被设置为零。例如: 0 1 0 0 0 2 5 0 3 0 0 0 0 0 0 0 5 0 2 1 对于每一个为零的矩阵元素,我想找到最近的元素的指数,它不是零。如果可能有多个索引,则应返回所有索引。这里有什么聪明的解决方案,如何避免大量for-loop内容?IPT中有一个高效的计算: 结果是: D = 1.0000 0 1.0000 1.0000 1.4142 0 0

在Matlab中,我有一个矩阵,其中一些元素被设置为零。例如:

0 1 0 0 0
2 5 0 3 0
0 0 0 0 0
0 5 0 2 1
对于每一个为零的矩阵元素,我想找到最近的元素的指数,它不是零。如果可能有多个索引,则应返回所有索引。这里有什么聪明的解决方案,如何避免大量for-loop内容?

IPT中有一个高效的计算:

结果是:

D =
    1.0000         0    1.0000    1.0000    1.4142
         0         0    1.0000         0    1.0000
    1.0000    1.0000    1.4142    1.0000    1.0000
    1.0000         0    1.0000         0         0

IDX =
           2           5           5          14          14
           2           6           6          14          14
           2           6           6          14          20
           8           8           8          16          20

返回的
IDX
包含
M
中最近非零值的线性索引。每个元素只返回一个索引。

这是一种矢量化方法,使用and存储每个单元格中每个零元素的非零最近元素的索引(通过欧几里德距离)-

%// Assuming A as the input matrix. Store rows, columns of zero and non-zeros
[rz,cz] = find(A==0);
[rnz,cnz] = find(A~=0);

%// Store zero pt indices
zero_pts = [rz cz];

%// Get squared euclidean distances
dists = bsxfun(@minus,rnz,rz.').^2 + bsxfun(@minus,cnz,cz.').^2;

%// Get all nearest XY indices of nonzeros for each zero pt
[R_idx,C_idx] = find(bsxfun(@eq,min(dists,[],1),dists));
idx = [rnz(R_idx) cnz(R_idx)];

%// Cut at each shifting positions and thus create a cell array, with a
%// cell of indices of non-zero nearest elements for each zero element 
nearest_nnonzero_pts = mat2cell(idx,histc(C_idx,1:max(C_idx)))
样本输入、输出-

输入:

>> A
A =
     0     1     0     0     0
     2     5     0     3     0
     0     0     0     0     0
     0     5     0     2     1
输出(零点):

输出(对应的最近非零点):


diff
可能会有帮助。您所说的最近距离是什么意思?垂直、水平、对角?@dean_sh:你列出的所有方向都应该考虑。好奇的是,贴出的解决方案对你有用吗?@Divakar:接受Amro的答案是因为它简单,但我也喜欢你的答案。从零开始学习如何实现这样的东西总是很有用的。距离变换是实现这一点的第一步。。。特别是因为有非常有效的实现,而不是暴力+从我这里得到1。
>> A
A =
     0     1     0     0     0
     2     5     0     3     0
     0     0     0     0     0
     0     5     0     2     1
>> disp(zero_pts)
     1     1
     3     1
     4     1
     3     2
     1     3
     2     3
     3     3
     4     3
     1     4
     3     4
     1     5
     2     5
     3     5
>> celldisp(nearest_nnonzero_pts)
nearest_nnonzero_pts{1} =
     2     1
     1     2
nearest_nnonzero_pts{2} =
     2     1
nearest_nnonzero_pts{3} =
     4     2
nearest_nnonzero_pts{4} =
     2     2
     4     2
nearest_nnonzero_pts{5} =
     1     2
nearest_nnonzero_pts{6} =
     2     2
     2     4
nearest_nnonzero_pts{7} =
     2     2
     4     2
     2     4
     4     4
nearest_nnonzero_pts{8} =
     4     2
     4     4
nearest_nnonzero_pts{9} =
     2     4
nearest_nnonzero_pts{10} =
     2     4
     4     4
nearest_nnonzero_pts{11} =
     2     4
nearest_nnonzero_pts{12} =
     2     4
nearest_nnonzero_pts{13} =
     4     5