You are on page 1of 15

Introduction to MATLAB Environment,

Math Functions, and Matrices

1.1 Program Outcomes (POs) Addressed by the Activity

a. Ability to apply knowledge of mathematics and science to solve engineering problems
b. Ability to identify, formulate, and solve engineering problems
c. Ability to use techniques, skills, and modern engineering tools necessary for engineering

1.2 Activitys Intended Learning Outcomes

a. Compute solutions to numerical problems in engineering using MATLABs interactive
computing environment and functions
b. Construct and manipulate vectors and matrices in MATLAB

1.3 Introduction
The entire digital signal processing (DSP) laboratory course uses MATLAB [1], a widely used technical
computing and visualization software system. Thus, the first couple of chapters in this material have
been dedicated to introducing the students to MATLAB environment, functions and programming. In
particular, this chapter focuses on important mathematical and matrix manipulation functions that we can
immediately employ in MATLABs command window, an interactive computing environment.

1.4 MATLAB Environment

To get started, open MATLAB from your program menu or through its shortcut icon.

[1] MATLAB is a registered trademark of The Mathworks, Inc.,

DSP Lab Manual Ronald M. Pascual & FEU Institute of Technology Page 3
Figure 1.1 shows how a typical MATLAB desktop looks like. Note that the desktops appearance varies with
different versions of MATLAB. In this section, we focus on the Command Window, an interactive command
line environment that allows us to enter and execute quick commands or computations just like how we
would use a calculator. The Command Windows prompt is the double greater than or the double right
arrowhead symbol (>>).

Fig. 1.1. A typical MATLAB Desktop

Suppose we want to compute the following: 1

Example 1.1 below shows a command that we may type at the Command Windows prompt, and shows
how MATLAB responds after pressing <Enter> or <Return>.

Example 1.1
>> 1+2/3*pi
ans =

DSP Lab Manual Ronald M. Pascual & FEU Institute of Technology Page 4
Like most common computing or programming languages, we use the arithmetic operator symbols
(+) for addition, (-) for subtraction, (*) for multiplication and (/) for division. Note that MATLAB assigns the
value of (3.14156) to the reserved symbol pi. Note also that the most recent result of a computation
is stored in a default variable named ans, just like in a typical calculator. We can use ans in our
subsequent computation as shown in Example 1.2.

Most of the time, we want to store the results of our calculations to our own user-defined variable
for future use. Example 1.2 also shows how we can quickly generate and employ user-defined variables
without the need for variable declaration. There are however some rules to keep in mind in creating user-
defined variables. For instance, we cannot create variable names that start with numbers, nor create
variable names that contains special characters such as plus sign (+), dash (-), asterisk (*), dot (.), slash
(/), commercial at (@), or even a space. Moreover, variable names in MATLAB are case- sensitive
(e.g., a variable named var_1 will be different from Var_1.)

Example 1.2
>> 1+2/3*pi
ans =
>> ans-2
ans =
>> var_1=1+2/3*pi
var_1 =

After doing Example 1.2, let us now turn our attention to another windowpane on MATLAB
Desktop called the Workspace window (shown in Fig. 1.2). The Workspace contains all the variables that
we have created so far in the current session of MATLAB. For example, Fig. 1.2 shows the variables ans
and var_1, and their respective values, sizes, number of bytes, and classes. Note that the class double
in MATLAB uses the 64-bit or 8-byte IEEE floating point format.
You may also generate the variable list directly from the Command Window using the command

Fig. 1.2. The Workspace window

DSP Lab Manual Ronald M. Pascual & FEU Institute of Technology Page 5
If we want to clear the Command Window, we may invoke the command clc. If we want to
clear variables from the workspace however, we may invoke the command clear.

Exercise 1.1
a.) Execute the sequence of commands in Example 1.2. Then enter the following command:

>> clc
>> whos

In the space provided below, write down MATLABs response after the whoscommand. Did clc clear
the variables in the workspace? Why or why not?

b.) Now, execute the following sequence of commands:

>> clc
>> clear ans
>> whos

In the space provided below, write down MATLABs response after the whoscommand. Did the
command clear ans clear all the variables in the workspace? Why or why not? If not, write the
command for clearing all the variables in the workspace.

DSP Lab Manual Ronald M. Pascual & FEU Institute of Technology Page 6
Another useful windowpane in the MATLAB desktop is the Command History window. The
Command History contains a chronological list of the commands you have executed in the Command
Window, including commands from past sessions in MATLAB. For instance, Fig. 1.3 shows the past
commands that we have executed so far in this Chapter.

Fig. 1.3. The Command History window

To quickly navigate through the past command list directly from the Command Window, use the
up/down arrows keys in your keyboard.

Getting help in MATALB may be done in different ways. For example, if you want to access the

complete product documentation, you may click on the help icon on the MATLAB toolstrip. But if
you want to quickly view a shortened text version of the function documentation in the Command
Window, use the help command as illustrated in Example 1.3 below.

Example 1.3
If we want to view the short text version of the command clc, we may enter the following command in

>> help clc

clc Clear command window.
clc clears the command window and homes the

See also home.

Reference page in Help browser

doc clc

DSP Lab Manual Ronald M. Pascual & FEU Institute of Technology Page 7
1.5 Math Operations and Functions
When performing a computation, MATLAB observes certain operator precedence (i.e., the order at
which multiple operations in one command line are performed.) A summary of MATLABs operator
precedence levels is shown in Table 1.1 below. Operators within same priority level are evaluated from
left to right as they appear in a command line.

Table 1.1 Operator Precedence in MATLAB

Priority Level Operations and Symbols

1 Parenthesis ( )
2 Power (^), Transpose ()
3 Multiplication (*), Division (/)
4 Addition (+), Subtraction (-)
5 Logical Operations such as:
Greater than (>), Less than (<), Equal to (==),
Greater than or equal to (>=), Not Equal to (~=)

Example 1.4
Enter the following command line in MATLAB:
>> 6+12/3*2^(2+1)
ans =
The result of the computation (i.e., 38) was obtained by performing operations in the following order:

1.) First, add 2 and 1, resulting to 3;

2.) Next, raise 2 to the power of 3 (obtained from step 1), resulting to 8;
3.) Then, divide 12 by 3, resulting to 4;
4.) Next, multiply 4 (obtained from step 3) by 8 (obtained from step 2), resulting to 32;
5.) Finally add 6 and 32 (obtained from step 4), resulting to 38.

Aside from arithmetic operators, MATLAB also has many mathematical functions available.
Some of the most common elementary math functions are listed below:
Various Sine Function: Other Trigonometric Functions:
sin sine of argument in radians cos - cosine
sind - sine of argument in degrees tan - tangent
sinh - hyperbolic sine sec - secant
asin - inverse sine in radians csc cosecant
asind - inverse sine in degrees cot cotangent
asinh - inverse hyperbolic sine

DSP Lab Manual Ronald M. Pascual & FEU Institute of Technology Page 8
(Note: Variations of the other trigonometric
functions are also available, e.g., atan for
inverse tangent.)
Complex Functions:
Exponential Functions: abs - absolute value/magnitude
exp - exponential angle phase angle
log - natural logarithm conj - conjugate
log10 - common logarithm
sqrt - square root

Example 1.5
Suppose we want to compute the following in MATLAB:
x cos(n)
y e 0.1n
z x y
where =2f, f=1/8 and n=3.
We may then enter the following sequence of commands in MATLAB command window, and observe
MATLABs response as shown below:
>> x=cos(2*pi*(1/8)*3)
x =
>> y=exp(-0.1*3)
y =
>> z=abs(x*y)
z =

Exercise 1.2
a.) Enter the following command in MATLAB command window :

>> 8*3/abs(-(1-4)^2/6*5)*7

What is the result of the preceding command? How was the result computed in the preceding
command? (Present an ordered list of operations just as in Example 1.4.)

DSP Lab Manual Ronald M. Pascual & FEU Institute of Technology Page 9
b.) Suppose we want to compute x, y and z below:

2p/ T
x ;
x sin(nx)
y ( )
z y 0.54 0.46cos( )

where Tp=(1/1500), Fs=8000, n=2, and L=46.

List down your MATLAB commands that computes x, y and z. What are the values of x, y and z?

1.6 Vectors and Matrices

Throughout the entire DSP laboratory course, we will be dealing with signal representations in the form
of vectors and matrices. For example a digital audio signal would usually be represented as a row or a
column vector, while a digital image would usually be represented as a matrix. Vectors may be thought
of as a special matrix having only a single row or a single column.

To create a row vector of any length in MATLAB, simply type the elements or numbers,
separated by commas or spaces, inside a square bracket [ ]. Similarly, to create a matrix of any size in
MATLAB, type the matrix elements separated by commas or spaces, and with each row separated by
semicolons, inside a square bracket [ ]. Note that the semicolon (;) is a row separator.

DSP Lab Manual Ronald M. Pascual & FEU Institute of Technology Page 10
Example 1.6
Suppose we want to create the following vectors and matrices in MATLAB:
A 1 2 3 B 0
4 3 6
C7 1 0

2 1 8

The following MATLAB commands will do the task:

>> A=[1,2,3]
A =
1 2 3
>> B=[1;0;-2]
B =
>> C=[4,-3,6;7,-1,0;2,-1,8]
C =
4 -3 6
7 -1 0
2 -1 8

Note that we may also create the column vector [B] by invoking the matrix transpose or single quote ()
operator as shown below:
>> B=[1,0,-2]
B =

Arithmetic operators (+), (-) and (*) also applies to vectors and matrices provided that proper
dimensions are observed. For example, addition (+) or subtraction (-) operators will only apply to vectors
and matrices that are having same dimensions. Otherwise, unequal dimensions results to a syntax error.
Similarly, multiplication (*) operator only applies between two matrices with equal inner or internal
dimensions. If what we want is an element-wise multiplication, instead of a matrix multiplication, then
we should precede the (*) operator with a dot (.). This is illustrated in the next example below.

DSP Lab Manual Ronald M. Pascual & FEU Institute of Technology Page 11
Example 1.7
Let us say we want to add the vectors [A] and [B]T from Example 1.6. The MATLAB command for this is:

>> A+B'
ans =
2 2 1

Now observe how MATLAB responds to the following operations:

>> A*B
ans =
>> A*B'
Error using *
Inner matrix dimensions must agree.
>> A.*B'
ans =
1 0 -6

Note from the preceding example that A*B is a matrix multiplication resulting to a scalar because the
outer dimensions of [A]1x3 and [B]3x1 are both equal to 1. The command A*B produced a syntax error
because the inner or interior dimensions of [A]1x3 and [B]T 1x3 are not equal (i.e., 3 1). Finally, the

operation A.*B is an element-by-element multiplication, and not a matrix multiplication.

Some useful matrix or array creation built-in functions, such as ones, zeros, rand and eye,
are available in MATLAB. These commands, together with their brief descriptions, are listed below:

eye - Identity matrix

linspace - Generate linearly spaced vectors
ones - Create array of all ones
rand - Uniformly distributed pseudorandom numbers
zeros - Create array of all zeros

Example 1.8
Let us look at the MATLABs response to the following commands:

>> A=ones(2,3)
A =
1 1 1
1 1 1
>> B=eye(3)
B =
1 0 0
0 1 0
0 0 1

DSP Lab Manual Ronald M. Pascual & FEU Institute of Technology Page 12
>> C=rand(1,4)
C =
0.8147 0.9058 0.1270 0.9134
>> D=linspace(0,2,5)
D =
0 0.5000 1.0000 1.5000 2.0000

In the preceding example: [A] is a 2-by-3 matrix all elements of which are equal to 1, [B] is a 3-by-3 identity
matrix, C is a 4-element row vector whose elements are random numbers, and [D] is a 5-point vector
whose elements are equally spaced in the range from 0 to 2.

The colon (:) or the range operator is one of the most useful operators in MATLAB that can
generate a vector whose elements are equally spaced, and can be used for array subscripting or a loop
iterator. The general form is (init : inc : final), where init = initial element value, inc = increment or
decrement value, and final = final element value.

Example 1.9
The matrix [D] created in Example 1.8 may likewise be created using the colon operator as shown below:

>> D=0:0.5:2
D =
0 0.5000 1.0000 1.5000 2.0000
>> E=1:5
E =

1 2 3 4 5
>> F=5:-1:1
F =
5 4 3 2 1

Note also from the preceding example that in creating the vector [E] where the element spacing is equal
to 1, we may delete the increment or the middle field from the command. In creating vector [F], note
that a negative value for the increment field is necessary.

Another important matrix manipulation task is extraction through array indexing. Array
indexing allows us to address a certain portion of a huge vector or matrix for extraction or isolation. In
signal processing for example, there are times when we need to perform an operation only to a short
segment of a long audio recording, or only to a small cropped portion of a huge matrix representing a
certain digital image.

To address a particular element of a certain matrix, say [A], specify its row and column number
using the following format:

A(row, col)

DSP Lab Manual Ronald M. Pascual & FEU Institute of Technology Page 13
Example 1.10
Let us now try the MATLAB commands shown below:

>> A=[1,2,3;4 5 0;0 -6 7]

A =
1 2 3
4 5 0
0 -6 7
>> B=A(2,1)
B =
>> C=A(2,:)
C =
4 5 0
>> D=A(1:2,2:3)
D =
2 3
5 0

After the execution of the second command in the preceding example, observe that [B] then contains
the 2nd row, 1st column element of [A]. After the execution of the third command, observe that [C]
then contains the whole 2nd row of [A]. Finally after the execution of the fourth command, check that
[D] contains the 2-by-2 portion of [A] corresponding to elements in rows 1 and 2, which are also in columns
2 and 3.

The final important matrix manipulation task covered in this chapter is matrix concatenation.
Matrix concatenation is the process of joining two or more matrices to make a larger matrix. In MATLAB,
brackets [ ] are used as concatenation operator. In the matrix concatenation task, we form a larger
matrix as we would form an ordinary vector or matrix, but this time with matrix elements being the smaller
matrices themselves to be concatenated.

Example 1.11
Let us now look at the following MATLAB commands:

>> A=[1,2,3]; B=[4,5,6];

>> C=[A,B]
C =
1 2 3 4 5 6
>> D=[A,A;B,B]
D =
1 2 3 1 2 3
4 5 6 4 5 6
We first note from the first command line in the preceding example that a semicolon (;) operator may
also be used as a statement separator, and as an output suppressor. Next, we see that [C] is a horizontal
concatenation of [A] and [B]. Finally, [D] is a result of both horizontal and vertical concatenations. That

DSP Lab Manual Ronald M. Pascual & FEU Institute of Technology Page 14
is, [D] is the vertical concatenation of [A,A] and [B,B], which are respective horizontal concatenations of
[A] and [B] with themselves.

Exercise 1.3
Write MATLAB commands that create a certain 7-by-7 matrix [A] with the following specifications:

1st row contains the sequence 0, 1, 2,, 6

sin(n / 2)
2nd row contains the values of , for n=0, 1, 2,,6
(n / 2)

3rd row contains the values 0.7 , for n=0, 1, 2,,6

4th row contains the element-wise product of rows 2 and 3

The lower right 3-by-3 portion of [A] are all zeros
The remaining elements are random numbers generated by rand

Present an ordered list of MATLAB commands with brief explanations for the above exercise. Is it
possible to change the order of your commands and still come up with the desired result? Why or why
not? Briefly explain your answer.

*** Machine Problem

The Lab Instructor provides the machine problem for this chapter.

DSP Lab Manual Ronald M. Pascual & FEU Institute of Technology Page 15
MATLAB Code/Notes:

DSP Lab Manual Ronald M. Pascual & FEU Institute of Technology Page 16
Results and Conclusion:

DSP Lab Manual Ronald M. Pascual & FEU Institute of Technology Page 17