Function to make a magic square matrix is taking too long?

function [magicMatrix] = magicSquare(n)
%Function that iterates through a matrix of dimension n x n
% to create a magic square, where n must be an odd number
% that is greater than or equal to 3
if mod(n,2) ~= 0 && n >= 3 %Checks to make sure the number is odd and greater or equal to 3
magicMatrix = zeros(n); %Creates a variable that holds a matrix of size n x n filled with zeros
x = (n - 1)/2 + 1; %Initializes the x position to the middle index of the matrix
y = 1; %Initializes the y position to be the first row of the matrix
iterator = 1; %Sets the initial number to start inputing into the matrix
matrixSize = n*n; %Gives the size of the matrix and the highest number included in the matrix
moveDown = true; %Boolean to see if we need to continue to move down
for i = 1:n %Loop to iterate through the entire matrix
magicMatrix(x,y) = iterator; %Sets the number in the position of the matirx
x = x - 1; %Decrement x to move up one
if x < 1 %Checks if x is on the boarder of the matrix
x = n;
end
y = y + 1; %Increment y to move to the right one
if y > n %Checks if y is on the boarder of the matrix
y = 1;
end
if magicMatrix(x,y) == 0 %Checks if the matix holds a zero at the position x,y
iterator = iterator + 1; %Increment the iterator
magicMatrix(x,y) = iterator; %Inserts the iterator number into the matrix at position x,y
else
x = x + 1; %Increment x to move down one
if x > n %Checks if x is on the boarder of the matrix
x = 1;
end
y = y - 1; %Decrement y to move left one
if y < 1 %Checks if y is on the boarder of the matrix
y = n;
end
while moveDown %Loop to see if we need to continue to loop down
x = x + 1; %Increment x to move down one
if x > n %Checks if x is on the boarder of the matrix
x = 1;
end
if magicMatrix(x,y) == 0 %Checks if matrix holds a 0 at position x,y
iterator = iterator + 1; %Increment the iterator
magicMatrix(x,y) = iterator;
moveDown = false;
end
if magicMatrix(x,y) ~= 0 %If matrix does not hold a zero at position x,y
moveDown = true;
end
end
end
iterator = iterator + 1; %Increments the iterator
end
else %Prints an error statement if the number entered is not odd or greater than or equal to 3
fprintf('Error: Must enter an odd number greater or equal to 3 \n');
end
end

1 Comment

When I call
magicSquare(3)
when i=3 and iterator=7 the while loop goes into an infinite loop, cycling forever over the values x=1,2,3,4, but nothing else changes (in particular the while condition). This means that your algorithm has a bug. You need to learn how to debug code, and fix your algorithm:

Sign in to comment.

Answers (1)

Try this:
function magicMatrix = magicSquare(n)
magicMatrix = magic(n)
Otherwise step through your code with the debugger and figure out where it's spending so much time.

6 Comments

Unfortunately I cannot use the built in function, but thanks
Why not?
And, in your function, you have decalred the function recursively.
After this line:
moveDown = true; %Boolean to see if we need to continue to move down
You have this line:
function [magicMatrix] = magicSquare(n)
That is probably causing problems. Why are you defining a function with the same name within a function???
The class I am taking wants us to make the function on our own. And that is an error that is on this page that it would not let me fix
I'll tag it as "homework" for you. And I never heard of an error that the editor will not let you fix. Can you explain more about that?
It is nothing to worry about, this webpage accidentally posted it twice, but I have fixed it now
I can't even get your code to run so I can't even time it. Why don't you look at the links to the right, where Cleve's blog goes over what looks like much simpler algorithms than yours?

Sign in to comment.

Categories

Asked:

on 12 Jan 2018

Edited:

on 12 Jan 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!