Getting Started Previous page   Next Page

The magic Function

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

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:

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

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


Previous page  The Colon Operator Expressions Next page

© 1994-2005 The MathWorks, Inc.