You have set of column indices and corresponding row index should be 1:1000.
There are two options to generate matrix from this.
accumarray
sparse
acccumarray will generate full matrix and sparse will generate sparse matrix.
In your case, the resulting matrix has density of 10% which can be considered as sparse matrix.
% Full matrix using accumarray ymat = accumarray([(1:1000).', y], ones(1000,1), [1000, 10]); % Sparse matrix using sparse ymat = sparse(1:1000, y, ones(1000,1), 1000, 10, 1000);
Computation time
I run the code with y = 1000 x 1 and y = 10000 x 1.
- y = 1000 x 1

- y = 10000 x 1

I added the code suggested by @transversality condition.
It turns out that accumarray is the fastest and sparse is the slowest.
accumarray shows better performance for small size, but the gap between accumarray and bsxfun(@eq) gets smaller as we increase the size of array.