The magic Function

May 8th, 2010 Posted in Functions

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

1
2
3
4
5
6
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.

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

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

1
2
3
4
5
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.

Leave a Reply

You must be logged in to post a comment.