MATLAB: Efficient Code for Filling Rows of an Array involving Indices Ranges

matrix manipulation

Hello,
I have an array A of zeros. I need to set a range of column indices for each row of A to ones. These ranges are determined by elements of another vector B, specifying start and end indices for the ranges for each row. For example, this is what I'm trying to figure out:
The first row of B indicates that the first row of A should have ones from columns 1 to 3. The second row of B indicates that the second row of A should have ones from columns 2 to 4 etc.
I could code this with a for-loop that goes through each of the rows and creates indices with the colon operator however, I'm hoping for a smarter, more efficient way to do it than that.
Can anyone help me with this or any suggestions?
Thanks.

Best Answer

e=1:size(A,2);
A = bsxfun(@ge,e,B(:,1)) & bsxfun(@le,e,B(:,2));