Matrix creation Octave / Matlab, loopless solution request
I want to create a matrix like A = [0 0 0 0 1; 0 0 0 1 1; 0 0 0 1 1; 0 0 0 1 1; 0 0 1 1 1; 0 1 1 1 1] based on a vector indicating how many '0's should precede '1's on each row: B = [4 3 3 3 2 1] Is there a loopless way to do this ? You don't mention in your question how the horizontal size of the array should be defined (the number of ones). For predefined width you can use this code: width = 5; A = cell2mat(arrayfun(@(x) [ zeros(1,x), ones(1,width-x) ], B, 'UniformOutput', false)'); If you want that A has minimal width, but still at least one 1 in every row: A = cell2mat(arrayfun(@(x) [