This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

Matrices and Magic Squares

About Matrices

In the MATLAB® environment, a matrix is a rectangular array of numbers. Special meaning is sometimes attached to 1-by-1 matrices, which are scalars, and to matrices with only one row or column, which are vectors. MATLAB has other ways of storing both numeric and nonnumeric data, but in the beginning, it is usually best to think of everything as a matrix. The operations in MATLAB are designed to be as natural as possible. Where other programming languages work with numbers one at a time, MATLAB allows you to work with entire matrices quickly and easily. A good example matrix, used throughout this book, appears in the Renaissance engraving Melencolia I by the German artist and amateur mathematician Albrecht Dürer.

This image is filled with mathematical symbolism, and if you look carefully, you will see a matrix in the upper-right corner. This matrix is known as a magic square and was believed by many in Dürer's time to have genuinely magical properties. It does turn out to have some fascinating characteristics worth exploring.

Entering Matrices

The best way for you to get started with MATLAB is to learn how to handle matrices. Start MATLAB and follow along with each example.

You can enter matrices into MATLAB in several different ways:

  • Enter an explicit list of elements.

  • Load matrices from external data files.

  • Generate matrices using built-in functions.

  • Create matrices with your own functions and save them in files.

Start by entering Dürer's matrix as a list of its elements. You only have to follow a few basic conventions:

  • Separate the elements of a row with blanks or commas.

  • Use a semicolon, ; , to indicate the end of each row.

  • Surround the entire list of elements with square brackets, [ ].

To enter Dürer's matrix, simply type in the Command Window

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

MATLAB displays the matrix you just entered:

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

This matrix matches the numbers in the engraving. Once you have entered the matrix, it is automatically remembered in the MATLAB workspace. You can refer to it simply as A. Now that you have A in the workspace, take a look at what makes it so interesting. Why is it magic?

sum, transpose, and diag

You are probably already aware that the special properties of a magic square have to do with the various ways of summing its elements. If you take the sum along any row or column, or along either of the two main diagonals, you will always get the same number. Let us verify that using MATLAB. The first statement to try is


MATLAB replies with

ans =
    34    34    34    34

When you do not specify an output variable, MATLAB uses the variable ans, short for answer, to store the results of a calculation. You have computed a row vector containing the sums of the columns of A. Each of the columns has the same sum, the magic sum, 34.

How about the row sums? MATLAB has a preference for working with the columns of a matrix, so one way to get the row sums is to transpose the matrix, compute the column sums of the transpose, and then transpose the result.

MATLAB has two transpose operators. The apostrophe operator (for example, A') performs a complex conjugate transposition. It flips a matrix about its main diagonal, and also changes the sign of the imaginary component of any complex elements of the matrix. The dot-apostrophe operator (A.'), transposes without affecting the sign of complex elements. For matrices containing all real elements, the two operators return the same result.




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



produces a column vector containing the row sums

ans =

For an additional way to sum the rows that avoids the double transpose use the dimension argument for the sum function:



ans =

The sum of the elements on the main diagonal is obtained with the sum and the diag functions:



ans =




ans =

The other diagonal, the so-called antidiagonal, is not so important mathematically, so MATLAB does not have a ready-made function for it. But a function originally intended for use in graphics, fliplr, flips a matrix from left to right:

ans =

You have verified that the matrix in Dürer's engraving is indeed a magic square and, in the process, have sampled a few MATLAB matrix operations. The following sections continue to use this matrix to illustrate additional MATLAB capabilities.

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:

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.

You can swap the two middle columns of B to look like Dürer's A. For each row of B, rearrange the columns in the order specified by 1, 3, 2, 4:

A = B(:,[1 3 2 4])
A = 
    16     3     2    13
     5    10    11     8
     9     6     7    12
     4    15    14     1

Generating Matrices

MATLAB software provides four functions that generate basic matrices.


All zeros


All ones


Uniformly distributed random elements


Normally distributed random elements

Here are some examples:

Z = zeros(2,4)
Z =
     0     0     0     0
     0     0     0     0

F = 5*ones(3,3)
F =
     5     5     5
     5     5     5
     5     5     5

N = fix(10*rand(1,10))
N =
     9     2     6     4     8     7     4     0     8     4

R = randn(4,4)
R =
    0.6353    0.0860   -0.3210   -1.2316
   -0.6014   -2.0046    1.2366    1.0556
    0.5512   -0.4931   -0.6313   -0.1132
   -1.0998    0.4620   -2.3252    0.3792
Was this topic helpful?