消除连续区域 [英] elimination of consecutive regions

查看:54
本文介绍了消除连续区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要有效地消除矢量"a"中的连续区域,或者更好地消除矩阵"A"的行/列中的区域,其单独区域的长度应大于正整数N <= length(A):

I need to effectively eliminate consecutive regions in vector "a" or better in rows/columns of matrix "A" with length of separate ones regions greater than positive integer N <= length(A):

请参见以下示例:

  N = 2    % separate consecutive regions with length > 2 are zeroed
  a =      [0 1 1 0 0 1 1 1 0 0 1 1 1 1 0 1]

  a_elim = [0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 1]

或2D情况:

  N = 2
  A =    [1 0 1 …
          1 1 0 …
          1 1 0 …
          0 0 1 …
          1 1 1]

  % elimination over columns
  A_elim= 0 0 1 
          0 1 0
          0 1 0
          0 0 1
          1 1 1
  % elimination over rows
  A_elim= 1 0 1 
          1 1 0
          1 1 0
          0 0 1
          0 0 0

我正在寻找有效的矢量MATLAB函数,以执行大小(A)〜[100000,1000](在列情况下)的任务.

I am looking for effective vectorized MATLAB function performing this task for size(A) ~ [100000, 1000] (over columns case).

推荐答案

您可以使用卷积:

对于一维情况:

N = 2 %tolerance
A =  [0 1 1 0 0 1 1 1 0 0 1 1 1 1 0 1] 

ind = conv(A,ones(N+1,1),'same');
%ind = 1  2  2  1  1  2  3  2  1  1  2  3  3  2  2  1
%A   = 0  1  1  0  0  1  1  1  0  0  1  1  1  1  0  1
ind = conv(ind>N,ones(N+1,1),'same')>0;
%ind = 0  0  0  0  0  1  1  1  0  0  1  1  1  1  0  0
%A   = 0  1  1  0  0  1  1  1  0  0  1  1  1  1  0  1
A(ind) = 0

如果N为奇数,则需要执行额外的步骤:

if N is odd you need an extra step:

ind = conv(A,ones(N+1,1),'same');
ind(find(ind==N+1)+1) = N+1 %the extra step
ind = conv(ind>N,ones(N+1,1),'same')>0;

nD维的概括:

N = 3 %tolerance
A =  round(rand(5,5,5));
for ii = 1:ndims(A)
    conv_vec = permute(ones(N+1,1),circshift([1:ndims(A)],ii-1,2))
    ind = convn(A,conv_vec,'same')
    if mod(N,2) == 1
        ind(find(ind==N+1)+1) = N+1
    end
    ind = convn(ind>N,conv_vec,'same')>0
    X = A;
    X(ind) = 0
end

这篇关于消除连续区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆