Chapter 3
Matrices, Operators, and Functions
The basic unit with which we work in Matlab is the matrix (plural is “matrices”). We add
them, subtract them, multiply them, and perform many other operations on them. In this
chapter, we introduce the basic idea of the matrix. We show how Matlab allows you to
define matrices and to operate on them. In addition to providing simple operations on
matrices, such as addition or subtraction, which are stated using the usual plus sign and
minus sign of mathematics, Matlab provides hundreds of other operations that are carried
out by calling built-in functions, such as
sqrt(x)
, which is a function that calculates
the square root.
3.1 Matrices
The name "MATLAB" stands for "Matrix Laboratory", which suggests (correctly) that
Matlab is designed to deal with matrices. A
matrix
is a 2-dimensional rectangular
arrangement of numbers, such as this “2x3”, matrix, which has 2-rows and 3-columns:
1
2
3
3.3
3.1416
-4
A matrix is a useful form for dealing with sets of two or more equations involving sets of
two or more variables, a situation that arises repeatedly in all branches of science and
engineering. Scientists and engineers also work with
scalars
(single numbers).
Surprisingly perhaps, Matlab treats a scalar as a 1x1 matrix! In fact even the
x
in
sqrt(x)
above is treated in Matlab as if it were a 1x1 matrix! To see the size of a
matrix in Matlab, you can use the built-in
function
called
size
:
>> size(x)
ans =
1
1
A
function
in mathematics is any operation that produces a result that depends only on its
input. In Matlab, as in most other programming languages (C, C++, Java, Fortran), a
function
is any operation that is invoked by giving its name. The major distinction
between these two definitions is that a mathematical function will
always
produce the
same output for a given input, whereas that is not necessarily true for a function provided
by a programming language. Input to a function is given as a list of values separated by
commas inside a pair of parentheses that follows the name. Each such value is called an
argument
(in both mathematics and programming). Here the
size
function was given
only one argument,
x
, (so no commas are needed), and it produced as its result two
numbers—1 and 1. The first 1 represents the number of rows, or height, of the matrix
x