Built-in functions
There are numerous built-in functions (i.e. commands) in MATLAB. We will mention a few of them in this section by separating them into categories.Scalar Functions
Certain MATLAB functions are essentially used on scalars, but operate element-wise when applied to a matrix (or vector). They are summarized in the table below.
sin trigonometric sine
cos trigonometric cosine
tan trigonometric tangent
asin trigonometric inverse sine (arcsine)
acos trigonometric inverse cosine (arccosine)
atan trigonometric inverse tangent (arctangent)
exp exponential
log natural logarithm
abs absolute value
sqrt square root
rem remainder
round round towards nearest integer
floor round towards negative infinity
ceil round towards positive infinity
Even though we will illustrate some of the above commands in what follows, it is strongly recommended to get help on all of them to find out exactly how they are used. The trigonometric functions take as input radians. Since MATLAB uses pi for the number
π = 3.1415…
» sin(pi/2)
ans =
1
» cos(pi/2)
ans =
6.1230e-017
The sine of π/2 is indeed 1 but we expected the cosine of π/2 to be 0. Well, remember that MATLAB is a numerical package and the answer we got (in scientific notation) is very close to
0 ( 6.1230e-017 = 6.1230×10 –17 ≈ 0).
Since the exp and log commands are straight forward to use, let us illustrate some of the other commands. The rem command gives the remainder of a division. So the remainder of 12 divided by 4 is zero
» rem(12,4)
ans =
0
and the remainder of 12 divided by 5 is 2.
» rem(12,5)
ans =
2
The floor, ceil and round commands are illustrated below.
» floor(1.4)
ans =
1
» ceil(1.4)
ans =
2
» round(1.4)
ans =
1
Keep in mind that all of the above commands can be used on vectors with the operation taking place element-wise. For example, if x = [0, 0.1, 0.2, . . ., 0.9, 1], then y = exp(x) will produce another vector y , of the same length as x, whose entries are given by y = [e0, e0.1, e0.2, . . ., e1].
» x = [0:0.1:1]
x =
Columns 1 through 7
0 0.1000 0.2000 0.3000 0.4000 0.5000 0.6000
Columns 8 through 11
0.7000 0.8000 0.9000 1.0000
» y = exp(x)
y =
Columns 1 through 7
1.0000 1.1052 1.2214 1.3499 1.4918 1.6487 1.8221
Columns 8 through 11
2.0138 2.2255 2.4596 2.7183
This is extremely useful when plotting data. See Section 2.4 ahead for more details on plotting. Also, note that MATLAB displayed the results as 1-by-11 matrices (i.e. row vectors of length 11). Since there was not enough space on one line for the vectors to be displayed, MATLAB reports the column numbers.
Vector Functions
Other MATLAB functions operate essentially on vectors returning a scalar value. Some of these functions are given in the table below.max largest component
min smallest component
length length of a vector
sort sort in ascending order
sum sum of elements
prod product of elements
median median value
mean mean value
std standard deviation
Once again, it is strongly suggested to get help on all the above commands. Some are illustrated below.
Let z be the following row vector.
» z = [0.9347,0.3835,0.5194,0.8310]
z =
0.9347 0.3835 0.5194 0.8310
Then
» max(z)
ans =
0.9347
» min(z)
ans =
0.3835
» sort(z)
ans =
0.3835 0.5194 0.8310 0.9347
» sum(z)
ans =
2.6686
» mean(z)
ans =
0.6671
The above (vector) commands can also be applied to a matrix. In this case, they act in a column by- column fashion to produce a row vector containing the results of their application to each column. The example below illustrates the use of the above (vector) commands on matrices Suppose we wanted to find the maximum element in the following matrix.
» M = [
0.7012,0.2625,0.3282
0.9103,0.0475,0.6326
0.7622,0.7361,0.7564];
If we used the max command on M, we will get the row in which the maximum element lies (remember the vector functions act on matrices in a column-by-column fashion).
» max(M)
ans =
0.9103 0.7361 0.7564
To isolate the largest element, we must use the max command on the above row vector. Taking advantage of the fact that MATLAB assigns the variable name ans to the answer we obtained, we can simply type
» max(ans)
ans =
0.9103
The two steps above can be combined into one in the following.
» max(max(M))
ans =
0.9103
Combining MATLAB commands can be very useful when programming complex algorithms where we do not wish to see or access intermediate results. More on this, and other programming features of MATLAB in Section ahead.
Matrix Functions
Much of MATLAB’s power comes from its matrix functions. These can be further separated into two sub-categories. The first one consists of convenient matrix building functions, some of which are given in the table below.eye identity matrix
zeros matrix of zeros
ones matrix of ones
diag extract diagonal of a matrix or create diagonal matrices
triu upper triangular part of a matrix
tril lower triangular part of a matrix
rand randomly generated matrix
Make sure you ask for help on all the above commands. To create the identity matrix of size 4 (i.e. a square 4-by-4 matrix with ones on the main diagonal and zeros everywhere else) we use the command eye.
» eye(4,4)
ans =
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
The numbers in parenthesis indicates the size of the matrix. When creating square matrices, we can specify only one input referring to size of the matrix. For example, we could have obtained the above identity matrix by simply typing eye(4). The same is true for the matrix building functions below. Similarly, the command zeros creates a matrix of zeros and the command ones creates a matrix of ones.
» zeros(2,3)
ans =
0 0 0
0 0 0
» ones(2)
ans =
1 1
1 1
We can create a randomly generated matrix using the rand command. (The entries will be uniformly distributed between 0 and 1.)
» C = rand(5,4)
C =
0.2190 0.3835 0.5297 0.4175
0.0470 0.5194 0.6711 0.6868
0.6789 0.8310 0.0077 0.5890
0.6793 0.0346 0.3834 0.9304
0.9347 0.0535 0.0668 0.8462
The commands triu and tril, extract the upper and lower part of a matrix, respectively. Let us try them on the matrix C defined above.
triu(C)
ans =
0.2190 0.3835 0.5297 0.4175
0 0.5194 0.6711 0.6868
0 0 0.0077 0.5890
0 0 0 0.9304
0 0 0 0
» tril(C)
ans =
0.2190 0 0 0
0.0470 0.5194 0 0
0.6789 0.8310 0.0077 0
0.6793 0.0346 0.3834 0.9304
0.9347 0.0535 0.0668 0.8
Once the extraction took place, the “empty” positions in the new matrices are automatically filed with zeros.
As mentioned earlier, the command diag has two uses. The first use is to extract a diagonal of a matrix, e.g. the main diagonal. Suppose D is the matrix given below. Then, diag(D) produces a column vector, whose components are the elements of D that lie on its main diagonal.
» D = [
0.9092 0.5045 0.9866
0.0606 0.5163 0.4940
0.9047,0.3190,0.2661];
» diag(D)
ans =
0.9092
0.5163
0.2661
The second use is to create diagonal matrices. For example,
» diag([0.9092;0.5163;0.2661])
ans =
0.9092 0 0
0 0.5163 0
0 0 0.2661
creates a diagonal matrix whose non-zero entries are specified by the vector given as input. (A short cut to the above construction is diag(diag(D)) ). This command is not restricted to the main diagonal of a matrix; it works on off diagonals as well. See help diag for more information. Let us now summarize some of the commands in the second sub-category of matrix functions.
size size of a matrix
det determinant of a square matrix
inv inverse of a matrix
rank rank of a matrix
rref reduced row echelon form
eig eigenvalues and eigenvectors
poly characteristic polynomial
norm norm of matrix (1-norm, 2-norm, ∞ -norm)
cond condition number in the 2-norm
lu LU factorization
qr QR factorization
chol Cholesky decomposition
svd singular value decomposition
Don’t forget to get help on the above commands. To illustrate a few of them, define the following matrix.
» A = [9,7,0;0,8,6;7,1,-6]
A =
9 7 0
0 8 6
7 1 -6
» size(A)
ans =
3 3
» det(A)
ans =
192
Since the determinant is not zero, the matrix is invertible.
» inv(A)
ans =
0.2812 -0.2187 -0.2187
-0.2187 0.2812 0.2812
0.2917 -0.2083 -0.3750
We can check our result by verifying that AA–1 = I and A–1A = I .
» A*inv(A)
ans =
1.0000 0.0000 0.0000
0.0000 1.0000 0.0000
0.0000 0.0000 1.0000
» inv(A)*A
ans =
1.0000 0.0000 0
0.0000 1.0000 0
0.0000 0 1.0000
Let us comment on why MATLAB uses both 0’s and 0.0000’s in the answer above. Recall that we are dealing with a numerical package that uses numerical algorithms to perform the operations we ask for. Hence, the use of floating point (vs. exact) arithmetic causes the “discrepancy” in the results. From a practical point of view, 0 and 0.0000 are the same. The eigenvalues and eigenvectors of A (i.e. the numbers λ and vectors x that satisfy Ax = λx ) can be obtained through the eig command.
» eig(A)
ans =
12.6462
3.1594
-4.8055
produces a column vector with the eigenvalues and
» [X,D]=eig(A)
X =
-0.8351 -0.6821 0.2103
-0.4350 0.5691 -0.4148
-0.3368 -0.4592 0.8853
D =
12.6462 0 0
0 3.1594 0
0 0 -4.8055
produces a diagonal matrix D with the eigenvalues on the main diagonal, and a full matrix X whose columns are the corresponding eigenvectors.