MATLAB: Efficient creation of a matrix with defined number of ones for each column

efficientones

I want to create a matrix B with a defined number of ones for each column.
So far I solve this problem like this – but I want to do it more efficiently (best without a loop), as I have a lot more columns in reality (3000+)
A = [1 5 3 6 10 7 8 9 2 1];
B = zeros(10:10);
n=1
while n<=size(B,2)
B(1:A(1,n),n) = ones(A(1,n),1);
n=n+1;
end

Best Answer

Taking advantage of implicit expansion:
A = [1 5 3 6 10 7 8 9 2 1];
B = double(A >= (1:10).')