For a vector V of size n x 1, I would like to create binary indicator matrix M of the size n x Max(V) such that the row entries of M have 1 in the corresponding columns index, 0 otherwise.
For eg: If V is
V = [ 3
2
1
4]
The indicator matrix should be
M= [ 0 0 1 0
0 1 0 0
1 0 0 0
0 0 0 1]
The thing about an indicator matrix like this, is it is better if you make it sparse. You will almost always be doing a matrix multiply with it anyway, so make that multiply an efficient one.
If you insist on M being a full matrix, then making it so is simple after the fact, by use of full.
Learn how to use sparse matrices. You will gain greatly from doing so. Admittedly, for a 4×4 matrix, sparse will not gain by much. But the example cases are never your true problem. Suppose that n was really 2000?
Sparse matrices do not gain only in terms of memory used. Compare the time required for a single matrix multiply.