How to fill a gap in a single width image?

M
Madalynn · Mar 1, 2021 · 1.9K views
Question
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 PhD Expert
Answered Aug 24, 2026
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;
100% Run Guarantee 3-Hour Fast-Track Delivery

Need a Custom Version or Complete Simulation for This Problem?

Our 500+ PhD engineers build, debug, and optimize working MATLAB scripts and Simulink (.slx) models tailored to your exact assignment rubrics with zero plagiarism.

Tested on MATLAB R2024b / R2026a
Turnitin 0% Plagiarism Report
Free 7-Day Revisions Guarantee
Have a different question? Ask here

Get a Free Consultation or a Sample Assignment Review!