필터 지우기
필터 지우기

Why is the output value a zero when it should be 1 for this program?

조회 수: 2 (최근 30일)
This function takes a matrix representing a 4x4 Sudoku board and returns true if the board is a valid Sudoku solution and false if it is not. The conditions for a valid board are: the numbers on the board should only be one of 1,2,3,4, also a number can not not be repeated on a row or column of the board or within any of the four 2x2 sub-blocks. Here is my code:
function out=issudoku(m)
[R,C]=size(m);
%Find if any number on the board is not a 1, 2, 3 or 4
for i=1:R
for j=1:C
if m(i,j)~=1||2||3||4
out=0;
%%%see if any number is repeated within the individual squares
elseif m(1,1)==m(2,1)||m(1,2)||m(2,2)
out=0;
elseif m(3,1)==m(4,1)||m(3,2)||m(4,4)
out=0;
elseif m(1,3)==m(2,3)||m(1,4)||m(2,4)
out=0;
elseif m(3,3)==m(4,3)||m(3,4)||m(4,4)
out=0;
%%repeated numbers in columns
elseif any(diff(m(:,1)==0))==1
out=0;
elseif any(diff(m(:,2)==0))==1
out=0;
elseif any(diff(m(:,3)==0))==1
out=0;
elseif any(diff(m(:,4)==0))==1
out=0;
%repeated numbers within rows:
elseif any(diff(m(1,:)==0))==1
out=0;
elseif any(diff(m(2,:)==0))==1
out=0;
elseif any(diff(m(3,:)==0))==1
out=0;
elseif any(diff(m(4,:)==0))==1
out=0;
else
out=1;
end
end
end
When i run issudoku([1 4 2 3; 3 2 4 1; 1 4 3 2; 2 3 1 4]) in the command window I am getting a 0 as a result when it should be 1.

채택된 답변

Jan
Jan 2016년 5월 5일
The line
if m(i,j)~=1||2||3||4
does not perform, what you expect. The condtion is evaluated from right to left:
1. m(i,j)~=1 ==> replies e.g. TRUE
2. (m(i,j)~=1) || 2 ==> this is TRUE || 2, which is TRUE in every case
3. TRUE || 3 ==> This is TRUE also
4. TRUE || 4 ==> guess
I assume you want:
if ~isember(m(i,j), 1:4)
or another formulation:
if m(i,j) > 4
or
if m(i,j)~=1 && m(i,j)~=2 && m(i,j)~=3 && m(i,j)~=4
  댓글 수: 1
Mohannad Abboushi
Mohannad Abboushi 2016년 5월 5일
Ok I changed that, but I am still getting false as a result for some reason

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Steven Lord
Steven Lord 2016년 5월 5일
In addition to what Jan said, the matrix you're testing is not a valid 4-by-4 Sudoku solution. Column 1 contains two 1's and no 4's, and column 2 contains two 4's and no 1's. Swapping the first two elements of either row 1 or row 3 will make it a valid Sudoku solution. I'd recommend swapping row 3 as then each of the main diagonal and antidiagonal will contain the digits 1 through 4 once as well.
[1 4 2 3;
3 2 4 1;
4 1 3 2;
2 3 1 4]
  댓글 수: 2
Mohannad Abboushi
Mohannad Abboushi 2016년 5월 5일
It is a valid Sudoku matrix, since all the conditions are satisfied in:
[1 4 2 3; 3 2 4 1; 1 4 3 2; 2 3 1 4]
Jan
Jan 2016년 5월 6일
But there are several 1s in the first column?!

댓글을 달려면 로그인하십시오.


Jan
Jan 2016년 5월 6일
Another problem:
elseif any(diff(m(1,:)==0))==1
What do you assume does this condition do?
m(1,:)==0 compares all elements of the 1st row with 0. Because the matrix contains the values 1 to 4, this is [true, true, true, true] in every case. Then you perform a diff. Because all elements are equal, this replies [0,0,0] in every case. Then any([0,0,0])==1 replies false. Note, that you can omit the "==1" part.
You see, that all these elseif any(... commands are not meaningful also.
Please fix this problem also and post your code again, such that we can search for further errors.
  댓글 수: 1
Mohannad Abboushi
Mohannad Abboushi 2016년 5월 6일
Here's my new code. When I put a matrix that has duplicate elements within its columns i.e. [1 4 2 3; 3 2 4 1; 1 4 3 2; 2 3 1 4]. It is still keeping out as true. While debugging it, I saw that it skipped assigning out as false after if a~=validnumbers.
function out=issudoku(m)
out=true;
validnumbers=[1,2,3,4];
[R,C]=size(m);
%check if sudoku board is 4x4
if R~=4||C~=4
out=false;
end
%check if any elements are greater than 4
if any(m>4)
out=false;
end
%check for any repeat values in columns
for j=1:C
a=m(:,j);
a=sort(a');
if a~=validnumbers
out=false;
end
end
%check for any repeat values in rows
for i=1:R
b=m(i,:);
sort(b);
if b~=validnumbers
out=false;
end
end
%check if any number is repeated within the individual squares
submatrix1=m([1 2],[1 2]);
if sort(reshape(submatrix1,1,[]))~=[1 2 3 4]
out=false;
end
submatrix2=m([3 4],[1 2]);
if sort(reshape(submatrix2,1,[]))~=[1 2 3 4]
out=false;
end
submatrix3=m([1 2],[3 4]);
if sort(reshape(submatrix3,1,[]))~=[1 2 3 4]
out=false;
end
submatrix4=m([3 4],[3 4]);
if sort(reshape(submatrix4,1,[]))~=[1 2 3 4]
out=false;
end

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Sudoku에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by