ismagical.m

来自「exm for Experiments with MATLAB (by Clev」· M 代码 · 共 30 行

M
30
字号
function m = ismagical(A)
% ISMAGICAL  Check various magic aspects of square matrices.
% m = ismagical(A) is a logical vector with four elements indicating:
% m(1) = Semimagic: all column sums and all row sums are equal.
% m(2) = Magic:  semimagic and both principal diagonals have the same sum.
% m(3) = Panmagic: magic and all the broken diagonals have the same sum.
% m(4) = Associative: all pairs of elements on oppositve sides of the
%        center have the same sum, which must be twice the center value.

[n,n] = size(A);
mu = (n^3 + n)/2;

semimagic = all(sum(A) == mu) && all(sum(A') == mu);

B = fliplr(A);
magic = semimagic && sum(diag(A)) == mu && sum(diag(B)) == mu;

panmagic = magic;
for d = 1:n
   panmagic = panmagic && ...
              sum([diag(A,d); diag(A,d-n)]) == mu && ...
              sum([diag(B,d); diag(B,d-n)]) == mu;
end

B = flipud(B);
associative = all(all(A+B == n^2+1));

m = [semimagic magic panmagic associative];

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?