MATLAB Answers

## How can I make magic matrix if I know the diagonal

Asked by Hussein Qenawy

### Hussein Qenawy (view profile)

on 7 Apr 2019
Latest activity Edited by John D'Errico

### John D'Errico (view profile)

on 7 Apr 2019
Accepted Answer by John D'Errico

### John D'Errico (view profile)

How can I make magic matrix if I know the diagonal for this matrix.. For example the diagonal 123..how can I build this magic matrix Thanks.

Walter Roberson

### Walter Roberson (view profile)

on 7 Apr 2019
There is no Magic Square that has 2 as its center element -- not unless negative numbers are permitted.
Hussein Qenawy

### Hussein Qenawy (view profile)

on 7 Apr 2019
First of all.. Thanks for your answer. If I need to create any magic matrix 3*3..and I know 3 number in this matrix.. How can I complete without guessing. Thanks
John D'Errico

### John D'Errico (view profile)

on 7 Apr 2019
Actually, there is a solution with no negative numbers, IF you allow some of the elements to be replicated. Many people would consider that not to be a true magic square though. And a common requirement is it must be composed of the integers 1:n^2.

Sign in to comment.

## 1 Answer ### John D'Errico (view profile)

Answer by John D'Errico

### John D'Errico (view profile)

on 7 Apr 2019
Edited by John D'Errico

### John D'Errico (view profile)

on 7 Apr 2019
Accepted Answer

Not all problems have a solution. But sometimes, they do, IF you are willing to relax some conditions. A classical magic square of order n is a permutation of the numbers 1:n^2, such that the sum of all rows and all columns, as well as the two diagonal sums, will all yield the same value. Clearly, at least something must be relaxed here for a result to exist.
Your question is to solve for the unknown variables a,b,c,d,e,f, such that the matrix
A = [1 a b ; c 2 d ; e f 3];
is a magic square. Does such a matrix exist for integer variables a,b,c,d,e,f? What if you allowed the variables to be negative? Walter claims out that no such matrix exists unless the variables can be negative. But does ANY matrix exist at all?
The sum of your main diagonal elements is 6. So we would need it to be true that:
sum(A,1) = [6 6 6]
And
sum(A,2) = [6;6;6]
AND don't forget the anti-diagonal sum.
b + 2 + e = 6
As such, you should notice that we are left with 7 linear equations, in 6 unknowns. That is usually a bad thing, suggesting the problem might indeed be unsolvable in general. At best, if a solution does exist, a purely integer solution might be unlikely. At least, we can try...
syms a b c d e f
A = [1 a b ; c 2 d ; e f 3]
A =
[ 1, a, b]
[ c, 2, d]
[ e, f, 3]
Time to turn this problem into a linear algebra problem.
[M,rhs] = equationsToMatrix([sum(A,1) == [6 6 6],sum(A.',1) == [6 6 6],b + 2 + e == 6],[a,b,c,d,e,f])
M =
[ 0, 0, 1, 0, 1, 0]
[ 1, 0, 0, 0, 0, 1]
[ 0, 1, 0, 1, 0, 0]
[ 1, 1, 0, 0, 0, 0]
[ 0, 0, 1, 1, 0, 0]
[ 0, 0, 0, 0, 1, 1]
[ 0, 1, 0, 0, 1, 0]
rhs =
5
4
3
5
4
3
4
We want to solve for the vector X = [a;b;c;d;e;f], such that M*X==rhs.
Linear algebra teaches us that a solution to such a problem exists if the right hand side vector is representable as a linear combination of the column space of the matrix M. First, what is the rank of M?
rank(M)
ans =
5
rank([M,rhs])
ans =
5
As long as these two rank computations return the same number, then a solution does exist. However, since the rank of M is only 5, then infinitely many solutions will exist. Even so, we might get lucky.
X = pinv(M)*rhs
X =
3
2
3
1
2
1
Plug it back into the matrix A.
Asol = subs(A,[a,b,c,d,e,f],X.')
Asol =
[ 1, 3, 2]
[ 3, 2, 1]
[ 2, 1, 3]
I think you will agree that this matrix is indeed a magic square, in the sense that it is entirely integer, that each row and each column, as well as the anti-diagonals all sum to 6.
Indeed, this is a magic square, subject to only one flaw - that the elements are not all distinct integers from the set 1:n^2. Is it possible to find a variation that allows all elements at least distinct integers? Even better, is it possible to find a solution that has all elements distinct and positive integers? The set of all solutions to the linear system posed can be written as
syms t
X = pinv(M)*rhs;
Xt = X + t*null(M)
Xt =
3 - t
t + 2
t + 3
1 - t
2 - t
t + 1
You can easily convince yourself that as long as t is any integer, then this will yield a solution, but that as well, for ANY non-zero value of t, at least some of those elements will either be the same, or some of them will be negative. So the fully general solution is given by At. Thus any integer value for t will result in a new magic sqaure with the given diagonal.
At = subs(A,[a,b,c,d,e,f],(X + t*null(M)).')
At =
[ 1, 3 - t, t + 2]
[ t + 3, 2, 1 - t]
[ 2 - t, t + 1, 3]
The two simplest magic squares with distinct elements, and a diagonal of [1 2 3] are:
subs(At,t,3)
ans =
[ 1, 0, 5]
[ 6, 2, -2]
[ -1, 4, 3]
subs(At,t,-3)
ans =
[ 1, 6, -1]
[ 0, 2, 4]
[ 5, -2, 3]
As one might expect, the two are actually the same matrices, just transposed. And, yes, there are negative numbers in the matrix.
So a solution does exist in positive numbers, IF you are willing to accept that the elements need not be distinct. If that is a problem, then negative numbers will be forced upon you. A nice thing is the methodology shown in this answer is extensible to matrices of a higher order, at least in theory.

#### 0 Comments

Sign in to comment.