As a first pass:
outIM = zeros(size(IM));
h = size(IM,1);
w = size(IM,2);
for Y = 1:h
L = round(l(Y));
R = round(r(Y));
p = round(interp1(linspace(L,R,w),1:w,1:w));
pv = ~isnan(p);
outIM(Y,p(pv),:) = IM(Y,L:R,:);
end
outIM = flipud(outIM);
This is far from ideal, as it pulls the values from individual complete pixels rather than some weighted color; likewise because it uses ragged edges rather than anti-aliasing the edges. Better yet would be a 2D interpolation.
I think it might be possible to vectorize the linspace and interp1 parts, but not the filling part, at least not with this quantized weighting.