Square (an introduction to matrices)
In Matlab, a matrix is a rectangular
array of numbers. A scalar
special 1-by-1 matrix, and matrices with only one row or column, are vectors. The
operations in Matlab are
designed to be as natural as possible. Some programming
languages work with numbers one at a time, but Matlab allows
you to work with entire matrices easily and quickly.
||There is a
good and famous matrix, which appears in the Renaissance
engraving Melencolia I, by the German artist Albrecht
|Durer's Magic Square
||The 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 Durer’s time to have
properties. It does have some interesting characteristics worth
The best way for you to get started with Matlab is to learn how to
handle matrices. You can enter matrices into Matlab in several
- Load matrices from external data files.
- Enter your explicit list of elements.
- Generate matrices using built-in
functions or with your own functions in m-files.
Enter Durer’s matrix as a list of elements. You only have to follow
a few conventions:
To enter Durer’s matrix, simply type in the Command Window
- Separate the elements of a row with
blanks or commas.
- Use a semicolon ';' to
end of rows.
- Surround the entire list of elements
with square brackets '[ ]'.
M = [16 3 2
13; 5 10 11 8; 9 6 7 12; 4 15 14 1]
Matlab displays the matrix you just entered.
16 3 2 13
5 10 11 8
9 6 7 12
4 15 14 1
This matches the numbers in the picture. Once you have entered the
matrix, you can refer to it simply as 'M'.
Now that you have M
workspace, take a look at what makes it so appealing. Is it a magic
You are probably 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 column or row, or along either of the two diagonals,
you will always get the same number. Let us verify that using Matlab.
The first statement to try is sum(M)
Matlab replies with
34 34 34
When you do not specify an output variable, Matlab uses the variable
to store the results of a calculation. You have found a row
vector containing the sums of the columns of M,
and each of the columns
has the magic sum,
How about the row sums? The easiest way to get the row sums is to
transpose the matrix, compute the column sums of the transpose, and
then transpose the result. The transpose
operation is denoted by an
It flips a matrix about its main
diagonal and turns a row vector into a column vector.
16 5 9 4
3 10 6 15
2 11 7 14
13 8 12 1
produces a column vector containing the row sums
The sum of the elements on the main diagonal is obtained with the sum
and the diag functions.
You have verified that the matrix in Durer’s engraving is indeed a
and have tried a few Matlab matrix operations.
The element in row i
and column j
is denoted by M(i,j).
is the number in the third row and second column. For
our magic square, M(3,2) is 6. So, to compute the sum of the elements
in the third column of M,
M(2,3) + M(3,3) + M(4,3)
It is also possible to refer to the elements of a matrix with a single
This is the common way of referencing row and column
vectors. But it can also apply to a fully two-dimensional matrix, in
which case the array is regarded as one long column vector formed from
the columns of the original
matrix. So, for our magic square, M(6) is another way of referring to
the value 10 stored in M(2,2).
The colon ‘:’
is one of the most important Matlab
operators. It occurs
in several different forms. The expression 1:8 is a row vector
containing the integers from 1 to 8
1 2 3 4
5 6 7 8
To obtain spacing, specify an increment. For example, 100 : -7 : 70
86 79 72
and 0 : pi/10 : pi/2
expressions involving colons refer to sections of a
is the first k
elements of the jth
column of M.
computes the sum of rows 1 to 4, along column 4.
But there's even a
better way. The colon by itself refers to all the elements in a row or
column of a matrix and
the keyword end
refers to the last row or column. So, sum(M(:,end))
computes the sum of the elements in the last column of M.
Matlab actually has a built-in function that creates magic squares.
This function is named 'magic'.
So, N = magic(4)
displays a 4-by-4 matrix
11 10 8
7 6 12
14 15 1
This matrix is almost the same as the one in the Durer's picture and
all the same magic properties; the only difference is that the
middle columns are exchanged. Noticed?
To make this N
into Durer’s M,
swap the two middle columns:
M = N(:, [1
3 2 4]). This means, for each of the rows of matrix N,
elements in the order 1, 3, 2, 4. It produces
10 11 8
6 7 12
15 14 1
'Magic Square' to
Square' to 'Matlab Help Menu'