How to fill a gap in a single width image?

Illustration
Madalynn - 2021-03-01T11:20:47+00:00
Question: How to fill a gap in a single width image?

Dear all, I have an 15X1 logical image for example like this:   0 0 0 1 1 0 0 0 1 1 1 1 0 0 0   As we see, the one's are not connected. I want to make a program that connect the gap between the one's similar the the imfill function in matlab but which can be applied in a single width image. In other words, how to get answer like this 0 0 0 1 1 1 1 1 1 1 1 1 0 0 0   I would appreciate it very much to hear some suggestions.

Expert Answer

Profile picture of John Williams John Williams answered . 2025-11-20

P = find(M, 1, 'first');   %here 1 is number of items to match, not value to match
Q = find(M, 1, 'last');
N = M;
N(P:Q) = 1;


Not satisfied with the answer ?? ASK NOW

Get a Free Consultation or a Sample Assignment Review!