MATLAB: I want to create a nxn matrix with all 0s and n randomly placed 1s

MATLABmatrix manipulation

I want to create a nxn zero matrix with n randomly placed ones in the matrix
Example
1st try
1 0 0
0 1 0
0 0 1
2nd Try
0 0 1
0 1 1
0 0 0

Best Answer

n = whatever;
x = zeros(n);
x(randperm(n*n,n)) = 1;