Asked by Hussein Qenawy
on 7 Apr 2019

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.

Answer by John D'Errico
on 7 Apr 2019

Edited by John D'Errico
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.

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 7 Comments

## madhan ravi (view profile)

Direct link to this comment:https://de.mathworks.com/matlabcentral/answers/454881-how-can-i-make-magic-matrix-if-i-know-the-diagonal#comment_690830

## Hussein Qenawy (view profile)

Direct link to this comment:https://de.mathworks.com/matlabcentral/answers/454881-how-can-i-make-magic-matrix-if-i-know-the-diagonal#comment_690853

## per isakson (view profile)

Direct link to this comment:https://de.mathworks.com/matlabcentral/answers/454881-how-can-i-make-magic-matrix-if-i-know-the-diagonal#comment_690855

## Hussein Qenawy (view profile)

Direct link to this comment:https://de.mathworks.com/matlabcentral/answers/454881-how-can-i-make-magic-matrix-if-i-know-the-diagonal#comment_690857

## Walter Roberson (view profile)

Direct link to this comment:https://de.mathworks.com/matlabcentral/answers/454881-how-can-i-make-magic-matrix-if-i-know-the-diagonal#comment_691019

## Hussein Qenawy (view profile)

Direct link to this comment:https://de.mathworks.com/matlabcentral/answers/454881-how-can-i-make-magic-matrix-if-i-know-the-diagonal#comment_691021

## John D'Errico (view profile)

Direct link to this comment:https://de.mathworks.com/matlabcentral/answers/454881-how-can-i-make-magic-matrix-if-i-know-the-diagonal#comment_691028

Sign in to comment.