Another solution:
L = prod(B+1); % length of the resulting array
output = zeros(L,length(B)); % preallocating memory
for ii = 1:length(B) % for each column of the output array
output(:,ii) = imresize(repmat(0:B(ii),1,prod(B(1:(ii-1))+1)), [1 L], 'nearest')';
end
Explanation:
repmat(0:B(ii),1,prod(B(1:(ii-1))+1)
Repeats the sequence 0:B(ii)
as many times as the product of all elements of B's before it. To take into account that the counting starts from zero we add +1
to all elements.
imresize(...,[1 L], 'nearest')';
Scales the vector then to the length of the array
Edit:
Version using interp1
instead of imresize
in case you don't have the image processing toolbox:
L = prod(B+1); % length of the resulting array
output = zeros(L,length(B)); % preallocating memory
for ii = 1:length(B) % for each column of the output array
p = prod(B(1:(ii-1))+1);
output(:,ii) = interp1(1:(((B(ii)+1)*p)), repmat(0:B(ii),1,p), linspace(1, (((B(ii)+1)*p)), L), 'nearest');
end