0

I have created a function for a magic square in matlab of size n, initialized with the zeros() command, and with a for loop using an iterator i.

The output I am getting is correct but I cannot figure out how to fix the issue of if the diagonal aka the place is out of bound for both row and col. in magic square(5), this number would be 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
user4888
  • 101
  • 1
  • 12

1 Answers1

0

The troubleshooting line, if row < 1 && col > n, had almost solved my problem. It just needed to be moved up to be before if row < 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
user4888
  • 101
  • 1
  • 12