i want to generate a matrix that the number in each row, colum contains 1-9,and not repeated.
my code always fall into no solution. here is my code.

orix = RandomSample[Range[1, 9]];
oriy = Join[{orix[[1]]}, 
   RandomSample[DeleteCases[Range[1, 9], orix[[1]]]]];
matrix = ConstantArray[0, {9, 9}];
matrix[[1]] = oriy;
matrix[[;; , 1]] = orix;
matrix // MatrixForm
Do[matrix[[x, y]] = 
   Range[0, 9] /. 
    Thread[Flatten[{matrix[[x]], matrix[[;; , y]]}] -> Nothing]], {x, 
  2, 9}, {y, 2, 9}]; matrix // MatrixForm

Thanks mr Moo ,Namjoshi ,and mikado the eligible matrix is much more than i thought. we can get a example matrix by:

matrix = ConstantArray[0, {9, 9}];
k = RotateRight[Range[9], 1];
Do[k = RotateLeft[k, 1];matrix[[i]] = k, {i, 9}]
matrix // MatrixForm

the other matrix can be got by changing the position of two numbers forexample /.{1->9,9->1}


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.