Use sub2ind
for this problem. Your y
determines the columns to use,
the rows are simply always incremented by 1. By using sub2ind
the desired row-column-combinations get transformed to a linear index and can then all be adressed in a vectorized way.
y = [3 1 5 3 4 2]; %// column indx
rows = 1:length(y); %// row indx
M = zeros(length(y), max(y)); %// A matrix full of zeros
M(sub2ind(size(M),rows ,y)) = 1; %// Ones at the desired row/column combinations