Sudokutxt contains a 9 times 9 matrix solution to a Sudoku p
Solution
check.m (File to do the checking)
function iscorrect = check(mat)
%row check
for j=1:9
vals = zeros(9);
for i=1:9
vals(i) = mat(i,j);
if vals(i)<=0 || vals(i)>9
iscorrect = false;
end
end
for p=1:9
for q=1:9
if (p~=q)
if vals(p) == vals(q)
iscorrect = false;
end
end
end
end
end
%column check
for j=1:9
vals = zeros(9);
for i=1:9
vals(i) = mat(j,i);
if vals(i)<=0 || vals(i)>9
iscorrect = false;
end
end
for p=1:9
for q=1:9
if (p~=q)
if vals(p) == vals(q)
iscorrect = false;
end
end
end
end
end
%block check
for j=1:9
vals = zeros(3,3);
for i=1:9
vals(mod(i,3)+1,mod(j,3)+1) = mat(i,j);
if vals(mod(i,3)+1,mod(j,3)+1)<=0 || vals(mod(i,3)+1,mod(j,3)+1)>9
iscorrect = false;
end
end
for p=1:3
for q=1:3
for r=1:3
for s=1:3
if (p~=r && q~=s)
if vals(p, q) == vals(r, s)
iscorrect = false;
end
end
end
end
end
end
end
iscorrect = true;
sudoku.m (File to load the sudoku and run the script)
sud = load(\'Sudoku.txt\');
mat = zeros(9,9);
for i=1:9
for j=1:9
mat(j,i) = sud(9*(i-1)+j);
end
end
if check(mat)==true
disp(\'The sudoku is correctly solved.\');
else
disp(\'Incorrect\');
end

