MATLAB: Elimination of conscutive regions (generalization: ones with zeros between)

consecutiveMATLABvectorization

I don't understand the question, or examples. The "number of consecutive "1's" between "-1's" is what it is. It's determined by A in advance and presumably A was created without regard to any N.
And for the outputs, you are changing the bottom of the middle column even though those 1's are not between ANY -1's. What's the correct rule?

Best Answer

A = [1 -1 0;
0 1 1;
0 1 1;
1 1 0;
0 0 1;
1 -1 0;
-1 1 1;
-1 0 -1;
1 1 1;
0 1 -1];
N = 3;
[m,n] = size(A);
Aclean = A;
for j=1:n
Aj = [-1; A(:,j); -1];
i = find(Aj == -1);
c = histc(find(Aj==1),i);
b = c <= N;
im = i(b);
ip = i([false; b(1:end-1)]);
a = accumarray(im,1,[m+2,1])-accumarray(ip,1,[m+2,1]);
mask = cumsum(a);
mask(i) = 1;
Aclean(:,j) = Aclean(:,j).*mask(2:end-1);
end
Aclean