Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Sudoku Matrix

Subject: Sudoku Matrix

From: Ryan

Date: 5 Mar, 2012 00:58:13

Message: 1 of 6

I am trying to generate a random matrix using only numbers 1-9 once each. I have figured out how to make one with the same total sum as the Sudoku matrix however it doesn't guarantee the conditions of a proper Sudoku matrix. Does any one have any ideas on how to create this random Sudoku matrix?

Subject: Sudoku Matrix

From: Roger Stafford

Date: 5 Mar, 2012 05:15:13

Message: 2 of 6

"Ryan" wrote in message <jj1335$8e8$1@newscl01ah.mathworks.com>...
> I am trying to generate a random matrix using only numbers 1-9 once each. I have figured out how to make one with the same total sum as the Sudoku matrix however it doesn't guarantee the conditions of a proper Sudoku matrix. Does any one have any ideas on how to create this random Sudoku matrix?
- - - - - - - - -
 I = [randperm(3),randperm(3)+3,randperm(3)+6];
 J = [3*randperm(3)-2,3*randperm(3)-1,3*randperm(3)];
 S = zeros(9);
 S(I+9*(J-1)) = randperm(9);

Roger Stafford

Subject: Sudoku Matrix

From: Roger Stafford

Date: 5 Mar, 2012 07:45:11

Message: 3 of 6

"Roger Stafford" wrote in message <jj1i51$ke1$1@newscl01ah.mathworks.com>...
> "Ryan" wrote in message <jj1335$8e8$1@newscl01ah.mathworks.com>...
> > I am trying to generate a random matrix using only numbers 1-9 once each. I have figured out how to make one with the same total sum as the Sudoku matrix however it doesn't guarantee the conditions of a proper Sudoku matrix. Does any one have any ideas on how to create this random Sudoku matrix?
> - - - - - - - - -
> I = [randperm(3),randperm(3)+3,randperm(3)+6];
> J = [3*randperm(3)-2,3*randperm(3)-1,3*randperm(3)];
> S = zeros(9);
> S(I+9*(J-1)) = randperm(9);
>
> Roger Stafford
-- - - - - - - - -
  I think I misunderstood what the problem is. If you use each of the numbers 1:9 just once each, then any random placement of these nine numbers anywhere in the matrix should be valid. Therefore the following should generate what you want:

 S = zeros(9);
 p = randperm(81);
 S(p(1:9)) = 1:9;

  If this isn't what you are asking for then could you please state what it is you consider "a proper Sudoku matrix".

Roger Stafford

Subject: Sudoku Matrix

From: dpb

Date: 5 Mar, 2012 15:07:42

Message: 4 of 6

On 3/5/2012 1:45 AM, Roger Stafford wrote:
...

> If this isn't what you are asking for then could you please state what
> it is you consider "a proper Sudoku matrix".

For a proper Sudoku matrix each column and each row must contain 1 thru 9

Only the first row (or column) is truly random; the rest are constrained
(more so as every row/column is selected) by the previous value(s) in
the specific row/column.

--

Subject: Sudoku Matrix

From: Roger Stafford

Date: 5 Mar, 2012 18:10:13

Message: 5 of 6

dpb <none@non.net> wrote in message <jj2kru$eoc$1@speranza.aioe.org>...
> For a proper Sudoku matrix each column and each row must contain 1 thru 9
> Only the first row (or column) is truly random; the rest are constrained
> (more so as every row/column is selected) by the previous value(s) in
> the specific row/column.
- - - - - - - - - -
  You speak of the final configuration with all cells filled, but Ryan has asked that we "generate a random matrix using only numbers 1-9 once each" starting with what I assume is an empty matrix. If I read that right, that leaves 72 cells unfilled and, since the integers 1-9 are all distinct from one another, places no restrictions whatever on where those first nine numbers are to be located.

  I have a feeling he might have meant something else that we haven't been told. Perhaps he wanted to generate a random final configuration with all cells filled using only the numbers 1-9 in which each appeared only once in each row, in each column, and in each of the nine 3 x 3 regions. That is quite a different request.

Roger Stafford

Subject: Sudoku Matrix

From: dpb

Date: 5 Mar, 2012 18:32:21

Message: 6 of 6

On 3/5/2012 12:10 PM, Roger Stafford wrote:
> dpb <none@non.net> wrote in message <jj2kru$eoc$1@speranza.aioe.org>...
>> For a proper Sudoku matrix each column and each row must contain 1 thru 9
>> Only the first row (or column) is truly random; the rest are
>> constrained (more so as every row/column is selected) by the previous
>> value(s) in the specific row/column.
> - - - - - - - - - -
> You speak of the final configuration with all cells filled, but Ryan has
> asked that we "generate a random matrix using only numbers 1-9 once
> each" starting with what I assume is an empty matrix. If I read that
> right, that leaves 72 cells unfilled and, since the integers 1-9 are all
> distinct from one another, places no restrictions whatever on where
> those first nine numbers are to be located.
>
> I have a feeling he might have meant something else that we haven't been
> told. Perhaps he wanted to generate a random final configuration with
> all cells filled using only the numbers 1-9 in which each appeared only
> once in each row, in each column, and in each of the nine 3 x 3 regions.
> That is quite a different request.

As soon as the word "Sudoku" was mentioned I presumed that would be the
case.

Perhaps he only wanted a starting grid; still it'll have to satisfy the
end constraints to be a solvable one.

--

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us