MATLAB: How to create a matrix from given vectors

matrixvector

I have a vector A= [ 2 4 1 3 ]
How can you create a matrix which are the length of the vector values with ones. the rest zeros?
i.e I want
B= [1 1 1 1; 1 1 0 1; 0 1 0 1; 0 1 0 0]
Regards
jason

Best Answer

C = zeros(max(A),numel(A));
C(A + (0:numel(A)-1)*size(C,1)) = 1;
B = flipud(cumsum(C(end:-1:1,:)));