简体   繁体   English

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

[英]Matlab - Find indices of nearest non-zero element for every zero matrix element

In Matlab, I have a matrix, where some elements are set to zero.在 Matlab 中,我有一个矩阵,其中一些元素设置为零。 Eg:例如:

0 1 0 0 0
2 5 0 3 0
0 0 0 0 0
0 5 0 2 1

For every matrix element which is zero I want to find the indices of the nearest element which is not zero.对于每个为零的矩阵元素,我想找到最近的非零元素的索引。 When multiple indices are possible, all of them should be returned.当可能有多个索引时,应返回所有索引。 What could be a clever solution here, how can I avoid a lot of for-loop stuff?这里有什么聪明的解决方案,我怎样才能避免很多 for 循环的东西?

There's an efficient bwdist function in IPT that computes the distance transform : IPT 中有一个高效的bwdist函数可以计算距离变换

M = [
    0 1 0 0 0
    2 5 0 3 0
    0 0 0 0 0
    0 5 0 2 1
];
[D,IDX] = bwdist(M~=0)

The result:结果:

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

The returned IDX contains linear indices of nearest nonzero value in M .返回的IDX包含M中最接近的非零值的线性索引 It only return one index per element.每个元素只返回一个索引。

Here's a vectorized approach using bsxfun and mat2cell that stores indices of non-zero nearest elements (by euclidean distance) for each zero element in a cell each -这是使用bsxfunmat2cell的矢量化方法,它为单元格中的每个零元素存储非零最近元素的索引(通过欧几里德距离) -

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

Sample input, output -样本输入、输出 -

Input :输入:

>> A
A =
     0     1     0     0     0
     2     5     0     3     0
     0     0     0     0     0
     0     5     0     2     1

Output (Zero points) :输出(零点):

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

Output (Corresponding nearest non-zero points) :输出(对应最近的非零点):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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