MATLAB: Create a matrix from 2 vectors

matrix vectorization

I have a vector A =[ 1;3;1;4] and a vector B = [ 8,9,9,6]. The values of A must increase one by one till reaching the value speficied in B, then, how can I obtain the following matrix:
[1 2 3 4 5 6 7 8 0; 3 4 5 6 7 8 9 0 0; 1 2 3 4 5 6 7 8 9; 4 5 6 0 0 0 0 0 0]
without using a for loop?
Thank your very much for your help

Best Answer

This creates your matrix with an expressed loop, however there are obviously loops within the functions.
The Code ā€”
A = [ 1;3;1;4];
B = [ 8,9,9,6];
C = ones(size(A,1), max(B));
C = cumsum(C,2);
C = bsxfun(@plus, C, A-1);
I = bsxfun(@le, C, B(:));
C = C.*I % Desired Result
C =
1 2 3 4 5 6 7 8 0
3 4 5 6 7 8 9 0 0
1 2 3 4 5 6 7 8 9
4 5 6 0 0 0 0 0 0
The ā€˜Cā€™ matrix is (obviously) the output.