In Matlab you don't have to write loop, you could write 2 lines

[i,j]=find(triu(ones(n)))
M=full(sparse(i,j,v,n,n))

and you are done. Way more compact