MATLAB: Does r have these many number of exact elements

loopsMATLAB

>> while a<15
a = rank(magic(n));
r(n) = a;
n=n+1;
end
>> r
r =
Columns 1 through 8
0 0 3 3 5 5 7 3
Columns 9 through 15
9 7 11 3 13 9 15

Best Answer

Here is your code.
r = [];
a = 0;
n = 3;
while a<15
a = rank(magic(n));
r(n) = a;
n=n+1;
end
I initialized a as 0, and r as an empty vector to allow the loop to operate properly.
I've assumed you started out n as at least 3, because magic(1) and magic(2) are not useful things. That is magic(2) is not in fact a magic square at all.
magic(2)
ans =
1 3
4 2
And magic(1) just returns the scalar 1.
So what is r after that loop? I'll append it to the numbers 1:n so we can look at what happens.
[1:numel(r);r]
ans =
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 0 3 3 5 5 7 3 9 7 11 3 13 9 15
That is, only for an index of [3,5,7,9,11,13,15] is the rank of the resulting magic suare full. In all even cases, magic does not generate a full rank matrix. (In fact, I often use this property of magoic to generate nice integer matrices that are not full rank in some of my answers.)
That is, if we look at the matrix magic(4), we see that it is indeed a magic matrix, with the necessary properties.
magic(4)
ans =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
>> sum(magic(4),1)
ans =
34 34 34 34
>> sum(magic(4),2)
ans =
34
34
34
34
>> sum(diag(magic(4)))
ans =
34
sum(diag(flip(magic(4))))
ans =
34
>> rank(magic(4))
ans =
3
So all rows and columns, and the two main diagonals sum to 34.
A quick search online finds the paper:
R. Bruce Mattingly (2000), Even Order Regular Magic Squares Are Singular, The American Mathematical Monthly, 107(9): 777-782.
The title tends to suggest that even order magic squares seem to have a problem.
But I also find the claim that some even order magic squares can be non-singular, see here:
In fact, in the next link, we find the statement that magic squares can be classified into three types, where the order is odd, singly even (divisible by 2, but not by 4), and doubly even (divisible by 4).
In fact, if you look at the code for magic,
type magic
you will see it does different things depending on the value of n.
To answer why it is that even order magic squares produced by magic are not full rank will be lengthy. But if we accept that even order magic squares are not full rank, then the simple answer to your question is why those numbers are repeated in the vector r. To answer that, we first look at the ranks of all doubly even magic squares as produced by magic. See that the ranks of magic(n), for n in the set [4 8 12...] seem to be always 3.
rank(magic(16))
ans =
3
>> rank(magic(20))
ans =
3
>> rank(magic(24))
ans =
3
>> rank(magic(32))
ans =
3
>> rank(magic(104))
ans =
3
You should see the pattern. The scheme used in magic to produce doubly even magic squares for doubly even values of n always produces squares of rank 3. So the number 3 will appear very regularly in the list of ranks in r. To prove that scheme always produces rank 3 matrices for doubly even n will take some minor effort, and I've already managed to make this a lengthy response.