Setting lone 1s to 0s in a logical vector/matrix
Show older comments
If I have this vector for example: [ 0 0 0 0 0 1 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0]
And I want to replace the lone 1 (the sixth element in this case) with a zero, is there an existing algorithm which can do this?
i.e. [ 0 0 0 0 0 1 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0] ----> [ 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0]
For context, this is an example of a row of pixels which has been colour thresholded to return 1s in areas of green and 0s otherwise.
The lone 1 represents an anomalous point, there is no green there and hence I wish to replace it with a 0.
For a further condition, I'd like to do the same for any section of 1s less than 3 in length.
i.e. [0 0 0 0 0 1 1 0 0 0 0 0 1 1 1 1 1 1 0 0 0 0] would go to [0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 0 0 0 0]
Accepted Answer
More Answers (1)
a = [0 0 0 0 0 1 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0];
[val, n] = RunLength(a);
del = (val == 1) & (n == 1); % Or: n < limit
val(del) = 0;
b = RunLength(val, n);
% b = [0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0];
1 Comment
Abbas Husain
on 3 Apr 2021
Categories
Find more on Image Arithmetic in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!