【问题标题】:Find the neighbors of all zeros elements in a matrix查找矩阵中所有零元素的邻居
【发布时间】:2014-03-25 18:21:51
【问题描述】:

在 matlab 中,我有一个非负条目的矩阵 A。见下一条:

A=[0 2 3 5 4 7 8
   1 8 2 7 5 2 9
   0 1 2 4 8 0 5
   2 4 8 6 0 5 8
   1 1 2 5 8 3 6];

我想找到所有零元素的邻居,除了零元素。这意味着我想将 A(1, 1)、A(2, 5)、A(3, 1)、A(3, 6)、A(4, 5) 和A(5, 1) 如果这些邻居之一为零,那么我不存储它。

元素(i, j)的邻居是指离(i, j)远一个元素的元素,即A(i, j+1), A(i, j-1), A(i-1, j), A(i-1, j-1), A(i-1, j+1), A(i+1, j), A(i+1, j-1) , 和 A(i+1, j+1)。每个元素 (i, j) 有 7 个邻居。

我不存储重复元素。这意味着,例如,如果 A(1, 1)=0 and A(1,3)=0 and A(1,2)=1 那么我将只存储 A(1, 2) 一次。

在我之前的示例中,向量 v 将是这个:

v=[2 1 8 1 2 4 5 2 9 8 5 5 8 4 6 5 8 3];

如何在matlab中做到这一点没有循环?

这是我的方阵代码:cl_ 是我的矩阵中零的个数。 ix_ 是零元素的行索引,iy_ 是零元素的列索引。

for i_=1:length(cl_)
    ixn1_(1:2)=ix_(i_);
    ixn2_(1:3)=ix_(i_)-1;
    ixn3_(1:3)=ix_(i_)+1;
    iyn1_=iy_(i_)-1;
    iyn2_=iy_(i_)+1;
    iyn3_(1:3)=[iy_(i_)-1, iy_(i_), iy_(i_)+1];
    ixn_{i_}=[ixn1_, ixn2_, ixn3_];
    iyn_{i_}=[iyn1_, iyn2_, repmat(iyn3_, 1, 2)];
end
% find the neighbors

【问题讨论】:

  • @LuisMendo 我已经更新了我的问题。

标签: matlab vector matrix


【解决方案1】:

如果您不关心值的顺序:

mask = A==0;
neighborMask = xor(conv2(double(mask),ones(3),'same')>0,mask);
v = A(neighborMask)

【讨论】:

    【解决方案2】:

    如果您想保持您在问题中陈述的顺序,可能就是这样 -

    代码

    %%// Given data
    A=[0 2 3 5 4 7 8
       1 8 2 7 5 2 9
       0 1 2 4 8 0 5
       2 4 8 6 0 5 8
       1 1 2 5 8 3 6];
    
    %%// Find indices that have value zero
    [indx,indy] = find(A==0);
    
    %%// OP seems to be travelling row-wise, so sort the indices column-wise
    [indx,y1] = sort(indx);
    indy = indy(y1);
    
    %%// Get the 8 neighboring row and column offsets  
    row1 = bsxfun(@plus,indx,[-1 -1 -1 0 0 1 1 1]);
    col1 = bsxfun(@plus,indy,[-1 0 1 -1 1 -1 0 1]);
    
    %%// Get the valid positions
    valid_pos = row1>0 & row1<=size(A,1) & col1>0 & col1<=size(A,2);
    
    %%// Get valid row and column numbers and then valid indices
    ind1 = (valid_pos.*col1-1).*size(A,1) + valid_pos.*row1;
    
    %%// Get valid linear indices
    ind1 = reshape(ind1',1,numel(ind1)); %%//'
    ind1 = ind1(ind1>0);
    
    %%// Find the unique indices without sorting
    [~, idxs, ~] = unique(ind1, 'first');
    ind1 = ind1(sort(idxs));
    
    %%// Get matrix values from indices and remove the zeros themselves
    OUT = A(ind1);
    OUT(OUT==0) = [];
    

    输出

    OUT =
    
         2     1     8     1     2     4     5     2     9     8     5     5     8     4     6     5     8     3
    

    【讨论】:

      【解决方案3】:

      首先,我假设您知道“查找”命令: 例如topbot=find(abs(zz)==H); 查找 zz 中所有等于 H 的元素并将它们存储在一个长向量“topbot”中。

      唯一不幸的是它将矩阵位置存储为单个数字,因此 (3,1) 存储为 3 等。因此,如果您想要行和列,则公式为 column = ceiling((number-1)/numrows)row = number mod numrows (不,语法不正确,但你明白了)。 (编辑:哎呀,数学超级难,一个错误无处不在)

      之后,只需删除重复项并确保不会得到负数的行或列。

      最后,矩阵值可以通过单个索引调用,所以如果 A 是 8 x 8 矩阵,A(15) 与调用 A(7,2) 相同。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-04-09
        • 1970-01-01
        • 1970-01-01
        • 2018-08-19
        • 1970-01-01
        相关资源
        最近更新 更多