1 view (last 30 days)

I need effectively eliminate (by zeroing) the consecutive "1's" between "-1's" and start/end of column at each column of matrix A, which now can be separated by any number of zeroes. The number of consecutive "1's" between "-1's" and start/end of column is > N. This is a non-trivial generalization of my previous Question.

Again, typical size(A) = [100000,1000].

See example:

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

For N = 2 the expected result is

Aclean = 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 -1 0 0 -1 0 -1 1 0 1 0 0 -1

For N = 3 the expected result is

Aclean = 1 -1 0 0 1 0 0 1 0 1 1 0 0 0 0 1 -1 0 -1 1 0 -1 0 -1 1 1 1 0 1 -1

Bruno Luong
on 1 Oct 2018

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

Bruno Luong
on 1 Oct 2018

Cleaner version

[m,n] = size(A);

Aclean = A;

for j=1:n

Aj = A(:,j);

im1 = find(Aj == -1);

i1 = find(Aj==1);

[c,loc] = histc(i1,[-Inf; im1; Inf]);

b = c > N;

Aclean(i1(b(loc)),j) = 0;

end

Sign in to comment.

Michal Kvasnicka
on 1 Oct 2018

Edited: Michal Kvasnicka
on 1 Oct 2018

Bruno Luong
on 2 Oct 2018

That's true, somehow it's a 1D scanning problem.

I think we are close to the limit of MATLAB can do, if faster speed is still needed, then one should go to MEX programming route instead of torturing MATLAB to squeeze out the last once of speed.

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 4 Comments

## Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/421655-elimination-of-conscutive-regions-generalization-ones-with-zeros-between#comment_616580

⋮## Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/421655-elimination-of-conscutive-regions-generalization-ones-with-zeros-between#comment_616580

## Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/421655-elimination-of-conscutive-regions-generalization-ones-with-zeros-between#comment_616590

⋮## Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/421655-elimination-of-conscutive-regions-generalization-ones-with-zeros-between#comment_616590

## Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/421655-elimination-of-conscutive-regions-generalization-ones-with-zeros-between#comment_616593

⋮## Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/421655-elimination-of-conscutive-regions-generalization-ones-with-zeros-between#comment_616593

## Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/421655-elimination-of-conscutive-regions-generalization-ones-with-zeros-between#comment_616596

⋮## Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/421655-elimination-of-conscutive-regions-generalization-ones-with-zeros-between#comment_616596

Sign in to comment.