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
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
추가 답변 (2개)
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
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.
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!