MATLAB: What is the function of this code segment in the whole code? Can any one explain

digital image processingimage segmentationmask

mask = zeros(length(img(:,1)), length(img(1,:)));
>for i=1:1:length(img(:,1))
>p1=find(img(i,:)~=0,1,'first');
>p2=find(img(i,:)~=0,1,'last');
> if(~isempty(p1) && ~isempty(p2))
> mask(i,p1:p2) = 1;
>end
>end

Best Answer

This poorly written code makes a mask where it scans each row and makes the mask "true" from the first non-zero pixel in the row to the last non-zero pixel in the row.