  function Jan23()
% Sudoku checker
clc
A = [5 3 4  6 7 8  9 1 2 ;...
     6 7 2  1 9 5  3 4 8 ;...
     1 9 8  3 4 2  5 6 7 ;...
%
     8 5 9  7 6 1  4 2 3 ;...
     4 2 6  8 5 3  7 9 1 ;...
     7 1 3  9 2 4  8 5 6 ;...
%
     9 6 1  5 3 7  2 8 4 ;...
     2 8 7  4 1 9  6 3 5 ;...
     3 4 5  2 8 6  1 7 9]
% Check a correct solution...
alfa = Sudoku(A);
fprintf('Sudoku(A) = %1d\n',alfa)
% This one is ok except columns 8 and 9 are bad...
A_bad1 = A; A_bad1(9,8:9) = [9 7]
alfa = Sudoku(A_bad1);
fprintf('Sudoku(A_bad1) = %1d\n',alfa)
% This one is ok except rows 8 and 9 are bad...
A_bad2 = A; A_bad2(8:9,9) = [9 5]
alfa = Sudoku(A_bad2);
fprintf('Sudoku(A_bad2) = %1d\n',alfa)
% This one is ok except blocks (1,2),(1,3),(2,2),(2,3),(3,2), and (3,3)
% are bad...
A_bad3 = A; A_bad3(:,[6 7]) = A_bad3(:,[7 6])
alfa = Sudoku(A_bad1);
fprintf('Sudoku(A_bad3) = %1d\n',alfa)

  function alfa = Sudoku(A)
% alfa is true if and only if A is a valid Sudoku solution matrix
[m,n] = size(A);
if m==9 && n==9
   % Each column of the following matrix must be (1:9)'...
   M = sort([A A' reshape(A(1:3,:),9,3) reshape(A(4:6,:),9,3) reshape(A(7:9,:),9,3)]);
   alfa = m==m && m==9 && max(max(M-(1:9)'*ones(1,27)))==0;
else
   alfa = 0;
end