MATLAB: How to use a vector as indices for matrix

vector indices

Hi,
Let's say I have a matrix NxM , and i have a vector 1xM. the contents of the vector indicates for every column in which row i should put the value '1'.
example – 5×5 matrix A all zeros, and vector V 1×5 = [3 1 1 5 4]. I want to get:
0 1 1 0 0
0 0 0 0 0
A = 1 0 0 0 0
0 0 0 0 1
0 0 0 1 0
How can I do that without using loops? (my matrix size is very large)
Thanks

Best Answer

V= [3 1 1 5 4]
m=numel(V);
n=max(V)
idx=sub2ind([n m],V,1:m)
out=zeros(n,m)
out(idx)=1