MATLAB: Print numbers on an array that have a unique row and column

array row column uniquechess

The code I wrote can print 1's on a unique column but can be on the same row. How could I get it to have a unique row?
for k=1:N % Loops N times
random = randi([1,N]); % Chooses a random number between 1 and board size
X(k,random) = 1; % Places a 'queen' on a random column
end

Best Answer

Perhaps you want:
index = randperm(8, 8);
X = zeros(8, 8);
for k = 1:8
X(k, index(k)) = 1;
end
0 0 0 0 0 0 1 0
0 1 0 0 0 0 0 0
0 0 0 0 0 0 0 1
0 0 0 0 0 1 0 0
0 0 0 0 1 0 0 0
1 0 0 0 0 0 0 0
0 0 0 1 0 0 0 0
0 0 1 0 0 0 0 0
Alternatively without a loop:
X = zeros(8, 8);
X(sub2ind([8,8], 1:8, randperm(8, 8))) = 1