Making a magic square matrix singular
Show older comments
We know that any magic square matrix of odd order is not singular. When each element of the matrix is subtracted by the sum-average of the total elements, then this perturbed matrix becomes singular, and the determinant of the resulted matrix is zero. That is,
det(magic(n)-ones(n)*((1+n*n)/2)) = 0, for any odd n.
Can anyone help me the proof or find literture in this subject?
10 Comments
Walter Roberson
on 1 Apr 2013
For, say, n = 9, then (1+n*n)/2 is 49, and ones(49) is a 49 x 49 matrix... which you try to subtract from the 9 x 9 matrix magic(9).
What is the "sum-average of the total elements" ? Is it (1 + 2 + 3 + ... n)/2 ? Is it (1 + 2 + 3 + ...) / n ? The first of those formulas is n * (n+1) / 2 / 2 or n * (n+1) / 4, and the second of those formulas is n * (n+1) / 2 / n or (n+1)/2 -- neither of which is (1+n*n)/2 . Perhaps you meant sum(sum(magic(n)) / n, which would be (n*n)*(n*n+1)/2/n^2 which would be (n*n+1)/2, the formula you gave? If so then test with det(magic(9)-41) and you will find the result is not 0.
Feng Cheng Chang
on 1 Apr 2013
Walter Roberson
on 1 Apr 2013
Edited: Walter Roberson
on 1 Apr 2013
Odd oh odd!!!
R2013a on OS-X:
>> n=9;det(magic(n)-ones(n)*(1+n*n)/2)
ans =
-0.4816269069579
R2012a on OS-X gives -0.3210846046386
I constructed the matrix and det() it on both sides to be sure that the same matrix was being used, but the det() differ!!! I checked with "which" to be sure I was only using the official MATLAB det.
det([6 17 28 39 -40 -29 -18 -7 4;16 27 38 -32 -30 -19 -8 3 5;26 37 -33 -31 -20 -9 2 13 15;36 -34 -23 -21 -10 1 12 14 25;-35 -24 -22 -11 0 11 22 24 35;-25 -14 -12 -1 10 21 23 34 -36;-15 -13 -2 9 20 31 33 -37 -26;-5 -3 8 19 30 32 -38 -27 -16;-4 7 18 29 40 -39 -28 -17 -6])
Matt J
on 1 Apr 2013
2. "sum-average of the total elements" means sum(sum(magic(n)))/n, which is (1+n*n)/2.
I think you really mean
sum(sum(magic(n)))/n^2 %divide by n^2
Feng Cheng Chang
on 1 Apr 2013
The matrix in det([... ]) is what I expected for the 9x9 perturbed matrix. Its determinant shold be equal to zero.
Feng, the determinant of the perturbed matrix is zero. Nobody disputes that and in fact, you've been given analytical proofs of why the matrix is singular.
The point, though, is that determinant computations are sensitive to floating point errors. I don't know how OCTAVE contends with that. What happens in OCTAVE when you compute the determinant as prod(eig(P))? In MATLAB, you get a very bad result,
>> P=magic(9)-41; prod(eig(P))
ans =
-2.2875
However, MATLAB's rank() command clearly knows that P is singular
>> rank(P)
ans =
8
Feng Cheng Chang
on 2 Apr 2013
Troyee
on 4 Feb 2026 at 23:26
create a 9*9 matrix with all element equal to 9
@Troyee: Please open a new question instead of appending it as comment to an old discussion. Thanks.
Then you will get a short solution like:
9 * ones(9, 9)
or
ones(9, 9) + 8
Accepted Answer
More Answers (3)
Jonathan Epperl
on 1 Apr 2013
2 votes
The row-sum, column-sum and diag-sum of a magic square are all the same, and the magic square uses all the integeres 1:n^2. Thus, the sum of all elements must be n^2*(n^2+1)/2, and each row, column, diag sum must be n*(n^2+1)/2.
Now look at what you wrote, multiply it from the right by ones(n,1), and you'll see that you will get zero. Voila, thus the matrix is singular.
2 Comments
Feng Cheng Chang
on 1 Apr 2013
Jonathan Epperl
on 5 Apr 2013
Edited: Jonathan Epperl
on 22 Apr 2013
Just to fill that hole in your knowledge: A square Matrix A is singular if and only if
- inv(A) does NOT exist
- det(A)==0
- The range of A is not all of R^n
- THE KERNEL OF A IS NONTRIVIAL
- ...
That last point there means that if you can find a nonzero vector v such that A*v==0, then you have proven that A is singular, and ones(n,1) is such a vector.
Let x=ones(n,1)/n and P be the perturbed matrix. You can verify that
P*x=0
proving that P is singular.
5 Comments
Feng Cheng Chang
on 1 Apr 2013
Feng, bear in mind that finite machine precision is in play here. You will not get P*x=0 exactly, but you will get very close
n=5;
x=ones(n,1)/n;
P=magic(n)-ones(n)*(1+n*n)/2;
>> norm(P*x)
ans =
4.5776e-16
Incidentally, for the same reasons also you will NOT get det(P)=0 exactly
>> isequal(det(P),0)
ans =
0
And, as Walter has shown you, the calculation of det(P) is numerically unstable. You can get highly nonzero-looking results for larger n on some machines. I also get similar results to his
>> det(magic(9)-41)
ans =
-0.3211
Here's more of an analytical proof. You know that the row-sums of all magic squares are the same. If S is the "sum-average", then the row-sums in terms of S are all S/n. Therefore
P*x= mean(magic(n) - ones(n)*S/n , 2)
= sum(magic(n),2)/n - sum(ones(n,1)*S/n , 2)/n
= ones(n,1)*S/n - ones(n,1)*S/n
= zeros(n,1)
Voila! And by the way, this is clearly true for all n, both even and odd.
Feng Cheng Chang
on 1 Apr 2013
I do not think the calculation of det(P) is numerically unstable since elements of P in the case are all integers.
You don't know that MATLAB computes determinants in steps that always result in integers. In fact, I've just learned that MATLAB uses LU factorization, which is consistent with the results Walter and I have been getting,
>> P=magic(9)-41; [L,U]=lu(P); det(L)*det(U)
ans =
-0.3211
Feng Cheng Chang
on 2 Apr 2013
Feng Cheng Chang
on 6 Apr 2013
Categories
Find more on Linear Algebra in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!