2017-05-13 2 views
0

Ich habe eine Funktion für ein magisches Quadrat in Matlab der Größe n, mit dem Nullen() Befehl initialisiert, und mit einer for-Schleife mit einem Iterator i erstellt.Create Funktion für magische Quadrat in Matlab

Die Ausgabe, die ich bekomme, ist korrekt, aber ich kann nicht herausfinden, wie das Problem zu beheben ist, wenn die Diagonale aka der Ort außerhalb der Reihe für Zeile und Spalte ist. in magischem Quadrat (5), würde diese Zahl 16 seine

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

Antwort

0

Die Fehlerbehebungszeile, wenn Reihe & col> n, hatte mein Problem fast gelöst. Es brauchte nur vor bewegt werden bis zu sein, wenn Zeile < 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