Matlab works with essentially one kind of
object: a matrix of numbers (which could include complex elements).
Scalars are 1-by-1 matrices, while vectores are 1-by-n or
When entering a matrix, seperate columns by spaces or commas and
rows by semicolons, e.g.:
>> P = [3 5 -6 1; -2 5 4 9]
3 5 -6 1
-2 5 4 9
Matlab stores the above 2-by-4 matrix in its
workspace for later use. To retrieve a variable, simply type its
name (e.g., P). Matlab is case-sensitive, so
p and P are not the
When you do not specify an output variable, Matlab uses the
variable ans, short for answer, to store the
results of a calculation.
Including a semicolon (;) at the end of a command suppresses
Matlab's echoing to the teminal (this is useful when dealing with
large sets of numbers.)
To take the sum along the columns of P
1 10 -2 10
Matlab has a preference for working with the
columns of a matrix, so the easiest way to get the row sums is to
transpose the matrix with the transpose operator .', compute the column sums of the transpose, and then
transpose the result:
In contrast single quote operator ' conjugates and transposes matrices (Hermitian
The element in row i
and column j of P is
denoted by P(i,j). If you try to use the value
of an element outside of the matrix, it is an error.
>> t = P(4,5)
Index exceeds matrix dimensions.
On the other hand, if you store a value in an
element outside of the matrix, the size increases to accommodate
>> X = P; X(1,5) = 17
3 5 -6 1 17
-2 5 4 9 0
It is also possible to refer to the elements
of a matrix with a single subscript, P(5). In this case the array
is regarded as one long column vector formed from the columns of
the original matrix.
Matlab provides mathematical expressions that
involve entire matrices. The building blocks of expressions are
variables, numbers, operators and
Matlab does not require any type declarations or dimension
statements for variables. Variable names consist of a
letter, followed by any number of letters, digits, or underscores.
Matlab uses only the first 31 characters of a variable name.
Expressions use familiar arithmetic operators and precedence
rules: + addition, - subtraction, * multiplication, / division,
division (described in Matrices and Linear Algebra in the MATLAB
^ power, ' complex conjugate transpose
and ( ) specify evaluation order. The mathematical operations
defined on matrices are the subject of linear algebra. The
multiplication symbol, *, denotes the matrix multiplication
involving inner products between rows and columns:
>> w = [0.5 1]; h = w*P
-0.5000 7.5000 1.0000 9.5000
Matlab uses conventional decimal notation,
with an optional decimal point and leading plus or minus sign, for
numbers. Scientific notation uses the letter e to specify a
power-of-ten scale factor. Imaginary numbers use either i or j as a
suffix. All numbers are stored internally using the long format
specified by the IEEE floating-point standard.
Matlab provides a large number of standard elementary mathematical
functions as well as more advanced mathematical functions,
including Bessel and gamma functions. Some of the functions, like
sqrt and sin or
are built in. Other functions, like gamma, sinh or hardlim, are implemented in M-files. You can see the
code, e.g. by typing
i Imaginary unit,
j Same as i
>> type hardlim
and even modify it if you want.