matlab majic,matlab magic

for n = 8:11

subplot(2,2,n-7)

surf(magic(n))

title(num2str(n))

axis off

view(30,45)

axis tight

end

45d6e600e675df267d16f85ff2aed3fc.png

其中,生成幻方的matlab程序如下

function M = magicV2(n)

%magicV2  magicV2 square.

%   magicV2(N) is an N-by-N matrix constructed from the integers

%   1 through N^2 with equal row, column, and diagonal sums.

%   Produces valid magicV2 squares for all N > 0 except N = 2.

%   Copyright 1984-2002 The MathWorks, Inc.

%   $Revision: 5.15 $  $Date: 2002/04/15 03:44:23 $

% Historically, MATLAB's magicV2 was a built-in function.

% This M-file uses a new algorithm to generate the same matrices.

n = floor(real(double(n(1))));

% Odd order.

if mod(n,2) == 1

[J,I] = meshgrid(1:n);

A = mod(I+J-(n+3)/2,n);

B = mod(I+2*J-2,n);

M = n*A + B + 1;

% Doubly even order.

elseif mod(n,4) == 0

[J,I] = meshgrid(1:n);

K = fix(mod(I,4)/2) == fix(mod(J,4)/2);

M = reshape(1:n*n,n,n)';

M(K) = n*n+1 - M(K);

% Singly even order.

else

p = n/2;

M = magicV2(p);

M = [M M+2*p^2; M+3*p^2 M+p^2];

if n == 2, return, end

i = (1:p)';

k = (n-2)/4;

j = [1:k (n-k+2):n];

M([i; i+p],j) = M([i+p; i],j);

i = k+1;

j = [1 i];

M([i; i+p],j) = M([i+p; i],j);

end

不同的矩阵的秩产生不同的幻方矩阵。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值