在Matlab中为魔术方创建函数

时间:2017-05-13 19:52:05

标签: matlab function magic-square

我已经在大小为n的matlab中为一个魔术方创建了一个函数,用zeros()命令初始化,并使用迭代器i进行for循环。

我得到的输出是正确的,但我无法弄清楚如何解决问题,如果对角线也就是这个地方对于行和列都没有界限。在魔方(5)中,这个数字是16。

magicsqure(5)

matrix =

 0     0     1     8    15
 0     5     7    14     0
 4     6    13     0     0
10    12     0     0     3
11     0     0     2     9



%
function output = magicsquare(n)

% initialize with zeros() function
matrix = zeros(n);

% place first number
col = (n-1)/2 + 1; % first number of magic square starts in the middle of top row
row = 1; % top row

% for loop with iterator i
for i = 1:(n^2) % start with 1 up to n^2 (perfect square of n sized matrix)
% if filled, move down on from original
if(matrix(row, col) ~= 0) % if a space in square is filled ... 
    row = row + 2; % ... move down 2 rows ...
    col = col - 1; % ... and 1 column to the left
end
% up one, right one
matrix(row, col) = i % input i at matrix(a,b) position
row = row - 1; % move up one row
col = col + 1; % move right one column

% out of matrix space -- wrap matrix
if col < 1 % if column goes to left of col 1
    col = n; % go to right most column
end
if row > (n+1) % if row goes down past last row by more than one space
    row = 2; % go to second row
end
if row > n % does not exist by one space
    row = 1; % go to first row
end
if col > n
    col = 1; % go to left most column
end
if row < 1
    row = n; % go to last row
end
if row < 1 && col > n %!! TROUBLESHOOTING
    row = 2;
    col = n;


end

output = matrix; % print the magic square
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

故障排除行,如果行&lt; 1&amp;&amp; col&gt; ñ几乎解决了我的问题。它只需要移动到之前,如果行&lt; 1.

% IMPORTANT: only works for odd numbers greater than 3
function output = magicsquare(n)

% initialize with zeros() function
matrix = zeros(n);

% place first number
col = (n-1)/2 + 1; % first number of magic square starts in the middle of top row
row = 1; % top row

% for loop with iterator i
for i = 1:(n^2) % start with 1 up to n^2 (perfect square of n sized matrix)
    % if filled, move down one from original
    if(matrix(row, col) ~= 0) % if a space in square is filled ... 
        row = row + 2; % ... move down 2 rows ...
        col = col - 1; % ... and 1 column to the left
    end
    % up one, right one method
    matrix(row, col) = i % input i at matrix(a,b) position
    row = row - 1; % move up one row
    col = col + 1; % move right one column
    
    % out of matrix space -- create wrap matrix
    if col < 1 % if column goes to left of col 1
        col = n; % go to right most column
    end
    if row > (n+1) % if row goes down past last row by more than one space
        row = 2; % go to second row
    end
    if row > n % does not exist by one space
        row = 1; % go to first row
    end
    if row < 1 && col > n % diagonal, out of bounds on both row and col
        row = 2;
        col = n;
    end
    if col > n
        col = 1; % go to left most column
    end
    if row < 1
        row = n; % go to last row
    end
end
output = matrix; % print the magic square
end