• Uncategorised

The magic Function

Matlab MAGIC Function

MATLAB actually has a built-in function that creates magic squares of almost any size.
Not surprisingly, this function is named magic.

B = magic(4)
B = 
    16     2     3    13
     5    11    10     8
     9     7     6    12
     4    14    15     1

This matrix is almost the same as the one in the Dürer engraving and has all the same “magic” properties; the only difference is that the two middle columns are exchanged.
To make this B into Dürer’s A, swap the two middle columns.

A = B(:,[1 3 2 4])

This says “for each of the rows of matrix B, reorder the elements in the order 1, 3, 2, 4.”
It produces

A = 
    16     3     2    13
     5    10    11     8
     9     6     7    12
     4    15    14     1

Why would Dürer go to the trouble of rearranging the columns when he could have used MATLAB’s ordering? No doubt he wanted to include the date of the engraving, 1514, at the bottom of his magic square.

You may also like...