How can i check if a matrix is magic square or not?
Mostrar comentarios más antiguos
I have to Develop a Matlab script to determine if the numbers stored in a square integer matrix form a magic square or not, without using loops !!
Respuesta aceptada
Más respuestas (4)
Refer to the definition of a magic square and test for that, i.e. sum the rows, columns and diagonal and see if they're all equal. I don't understand what difficulty there is.
Image Analyst
el 24 de Nov. de 2017
0 votos
Hints:
Get the size with size() and make sure that there are only two dimensions (e.g. it's not a 3-D or 4-D matrix) and the length of each of those dimensions are equal (i.e., it's square).
If it's square then create a magic square with magic(rows).
Use isequal() to compare your matrix to the "official" one. You also have to use the transpose operator and the flipud() and fliplr() functions to test if any rotation or mirror image is equal. If any of those orientations is equal, then it's a magic square.
The other approach is to call sum(m,1) and sum(m,2) and then see if all values are the same, like with the all() or diff() function - lot of ways to do this. Then use eye() to sum the diagonals. You'll need to use fliplr() to get the diagonal from upper right to lower left.
4 comentarios
Using isequal to compare against the MATLAB magic square of a given size will not work, because of how many magic squares there are: "Excluding rotations and reflections, there is exactly one 3×3 magic square, exactly 880 4×4 magic squares, and exactly 275,305,224 5×5 magic squares. For the 6×6 case, there are estimated to be approximately 1.8 × 1019 squares"
The proposed method would only match one of these for each size. What about all the others?
"The other approach ..."
is the only approach.
Image Analyst
el 24 de Nov. de 2017
I said to do rotations and flips. As Wikipedia says "Any magic square can be rotated and reflected to produce 8 trivially distinct squares." so I was checking for all those 8 possibilities. And isequal() would work for that. I'm not saying that's the best or fastest or most efficient approach, merely another approach.
"I said to do rotations and flips"
Totally irrelevant. The translations and reflections are not the reason why this concept is flawed. Simply put:
- MATLAB only generates one magic square for any size (using a deterministic algorithm).
- There are many possible magic squares for each size >= 4x4 (disregarding translations and reflections).
- Your concept therefore only checks for one of those possible magic squares: the one that MATLAB happens to generate.
- Hence your concept incorrectly identifies all of the other magic squares as not being magic.
Lets demonstrate with a simple random 4x4 magic square (NOT from MATLAB):
>> M = [2,8,11,13;10,16,5,3;15,9,4,6;7,1,14,12]
M =
2 8 11 13
10 16 5 3
15 9 4 6
7 1 14 12
>> magic(4) % compare to MATLAB
ans =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
Are they the same? A simple check by eye will confirm that these are not translations or reflections of each other. Ditto for another 878 different 4x4 magic squares which your concept incorrectly identifies as not being magic. Your concept will correctly identify just one of those 880 4x4 magic squares as being magic: the one the MATLAB happens to generate.
The task requests to "determine if the numbers stored in a square integer matrix form a magic square or not". Your concept incorrectly identifies almost every magic square that exists as being non-magic (except for the small subset that MATLAB can generate). Therefore it does not fulfill the requirements of the task, because it will incorrectly identify almost 100% of all magic squares as not being magic: your concept will correctly identify just one of the estimated 1.8e19 6x6 magic squares, and is thus quite close to 0% correct.
Simple summary: the concept does not work because there are many magic squares for any size >= 4x4. Translations and reflections are totally irrelevant to this issue.
Image Analyst
el 25 de Nov. de 2017
Thank you - I understand now.
Jos (10584)
el 24 de Nov. de 2017
0 votos
You can also take a look at chapter 10, titled "Magic Squares", of the book Experiments with Matlab, by Cleve Moler: https://uk.mathworks.com/moler/exm.html
Very informative reading regarding this question :)
Thomas
el 21 de Jun. de 2023
0 votos
try:
function ismagic = ismagic(M)
%ISMAGIC checks if a matrix M is a magic square or not
if size(M,1) ~= size(M,2)
ismagic = false;
return;
else
Msums = [sum(M,1)'; sum(M,2); sum(diag(M)); sum(diag(flip(M)))]; % create an array containing all 2*n + 2 sums to be checked
if max(Msums) ~= min(Msums)
ismagic = false;
else
ismagic = true;
end
return;
end
end
1 comentario
Stephen23
el 22 de Jun. de 2023
The basic concept is nice. A few tips:
- best not to confuse things using the same output variable name as the function itself.
- RETURN is not required (MATLAB functions return at the end).
- instead of IF ELSE just allocate the logical directly as the output.
function X = ismagic(M)
% ISMAGIC checks if a matrix M is a magic square or not
X = false;
if isequal(diff(size(M)),0) % ISMATRIX
V = [sum(M,1).';sum(M,2);sum(diag(M));sum(diag(flip(M)))];
X = max(V)==min(V);
end
end
Categorías
Más información sobre Logical en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!