MATLAB
INTRODUCTION TO
MATLAB
What is MATLAB?
MATLAB (MATrix LABoratory) is a special-purpose
computer program optimized to perform engineering
and scientific calculations.
It is a high-performance language for technical
computing.
It integrates computation, visualization, and
programming in an easy-to-use environment
Typical uses include:
Math and computation
Algorithm development
Modelling, simulation and prototyping
Data analysis, exploration and visualization
Scientific and engineering graphics
Application development, including Graphical User
Interface (GUI) building
What is MATLAB?
MATLAB is an interactive system
whose basic data element is an array
that does not require dimensioning.
This allows the capabilities to solve
many technical computing problems.
Especially those with matrix and vector
formulations.
MATLAB features
family of application-specific solution called
Toolboxes.
Toolboxes are comprehensive collections of
MATLAB function (m-files) that extend the MATLAB
environment to solve particular classes of problems.
Areas in which toolboxes are available:
signal processing
control systems
neural networks
fuzzy logic
Wavelets
image processing
simulation and many others.
MATLAB
Getting Started
Starting MATLAB
Double-clicking on the MATLAB icon or
invoking the application from the Start
menu of Windows.
MATLAB 7.0.1.lnk
Starting MATLAB
The Command Window
The Command Window is where the command line prompt for
interactive commands is located.
The “>>” is called the Command Prompt
Example
Write a simple MATLAB command,
which is the date command.
MATLAB should then return something
like this:
>> date
ans =
22-Sep-2007
MATLAB
MATLAB Basics
Variables and Arrays
The fundamental unit of data in any
MATLAB program is the array.
Even scalars are treated as arrays by
MATLAB
There are three fundamental concepts
in MATLAB, and in linear algebra
-scalars, vectors and matrices.
Variables and Arrays
A scalar = a single value.
A vector = an ordered list of numbers (one-
dimensional). In MATLAB they can be
represented as a row-vector or a column-
vector.
A matrix = a rectangular array of numbers
(multi-dimensional). In MATLAB, a two-
dimensional matrix is defined by its number
of rows and columns.
Example
a 10 This is a scalar, containing 1 element
b 1 2 3 4
This is a 14 array containing 4 elements,
known as a row vector
1
This is a 31 array containing 3 elements, known
c 2 as a column vector
3
1 2 3
This is a 33 matrix, containing 9
d 4 5 6 elements
7 8 9
More example
>> my_scalar = 3.1415 >> my_vector1 = [1 5 7]
my_scalar = my_vector1 =
3.1415 1 5 7
>> my_vector1 = [1, 5, 7]
my_vector1 = >> my_vector2 = [1
5
1 5 7 7]
>> my_vector2 = [1; 5; 7] my_vector2 =
my_vector2 = 1
5
1 7
5
7
>> my_matrix = [8 12 19; 7 3 2; 12 4 23; 8 1 1]
my_matrix =
8 12 19
7 3 2
12 4 23
8 1 1
examples
A semicolon (;) is used to hide the
details of any operation in the
command window
E.g
>> x =2 ;
>>y = 3;
>> c = x + y
>> c =
5
Indexing into an Array
A 2-by-3 Matrix
Row 1
Row 2
Col 1 Col 2 Col 3
Individual elements in an array are addressed by the array name
followed by the row and column of the particular element.
If the array is a row or >> my_vector2 = [1; 5; 7]
column vector, then only one
subscript is required. my_vector2 =
For example, 1
my_vector2(2) is 5 5
7
my_matrix(3,2) is 4 or
my_matrix(7) is 4
The Colon Operator
The colon (:) is one of MATLAB’s most important operators.
To create an incremental or a decrement vector
>> my_inc_vec1 = [1:7]
my_inc_vec1 =
>> my_dec_vec = [5:-2:1]
[ 1 2 3 4 5 6 7]
my_dec_vec =
[ 5 3 1]
>> my_inc_vec2 = [1:2:7]
my_inc_vec2 =
[ 1 3 5 7]
The Colon Operator
To refer portions of a matrix/vector.
>> my_matrix = [8 12 19; 7 3 2; 12 4 23; 8 1 1]
my_matrix =
8 12 19
7 3 2
12 4 23
8 1 1
>> new_matrix1 = my_matrix(1:3,2:3)
new_matrix1 =
12 19
3 2
4 23
>> new_matrix2 = my_matrix(2:4,:)
new_matrix2 = NOTES: If the colon is used by itself
7 3 2 within subscript, it refers to all the
12 4 23 elements in a row or column of a matrix!
8 1 1
Concatenating Matrices
>> A = [8 19; 7 2];
>> B = [1 64; 4 5; 3 78];
>> C = [A; B]
C =
8 19
7 2
1 64
4 5
3 78
Matrix concatenation is the process of joining one or
more matrices to make a new matrix.
The expression C = [A B] horizontally concatenates
matrices A and B. The expression C = [A; B]
vertically concatenates them.
Reshaping a Matrix
Reshape 3-by-4 matrix A to have dimensions 2-by-6.
>> A = [1 4 7 10; 2 5 8 11; 3 6 9
12]
A =
1 4 7 10
2 5 8 11
3 6 9 12
>> B = reshape(A, 2, 6)
B =
1 3 5 7 9 11
2 4 6 8 10 12
General Function for Matrix
and Vector
Basic Vector Function
size Returns the dimensions of a matrix
length Returns the number of elements in a matrix
min Returns the minimum value contained in a matrix
max Returns the maximum value contained in a matrix
sum Returns the sum of the elements in a matrix
sort Returns the sorted elements in a matrix
abs Returns the absolute value of the elements in a matrix
Examples
>> A = [3 1 2 4]; >> mnA = min(A) >> sumA = sum(A)
>> szA = size(A)
mnA = sumA =
szA =
1 10
14 >> mxA = max(A)
>> stA = sort(A)
>> lenA = length(A) mxA =
stA =
lenA = 4
1 2 3 4
4
Functions to Create a Matrix
diag Create a diagonal matrix from a vector
cat Concatenate matrices along the specified dimension
ones Create a matrix of all ones
zeros Create a matrix of all zeros
rand Create a matrix of uniformly distributed random numbers
repmat Create a new matrix by replicating or tiling another
Examples
>> A = zeros(2,4) >> C = rand(4,3)
A = C =
0 0 0 0 0.9501 0.8913 0.8214
0 0 0 0 0.2311 0.7621 0.4447
0.6068 0.4565 0.6154
>> B = 7*ones(1,3) 0.4860 0.0185 0.7919
B =
7 7 7
Arithmetic Operations
MATLAB can be used to evaluate
simple and complex mathematical
expressions.
Can perform some operations either
on an element-by-element (array
operation) or
matrices as whole entities (matrix
operation).
Array Operators
Operation MATLAB Form Comments
Addition A + B Array addition is identical
Subtraction A - B Array subtraction is identical
Multiplicati A .* B Element-by-element multiplication of A
on and B. Both arrays must be the same
shape, or one of them must be a scalar
Division A ./ B Element-by-element division of A and B.
Both arrays must be the same shape, or
one of them must be a scalar
Power A .^ B Element-by-element exponentiation of A
and B. Both arrays must be the same
shape, or one of them must be a scalar
Examples
>> A = [1 4 7 10; 2 5 8 11; 3 6 9 12] >> A = [ 2 4 ; 8 10]
A =
A =
1 4 7 10 2 4
2 5 8 11 8 10
3 6 9 12
>> B = [2 4; 2 5]
>> B = [1 2 3 4; 5 6 7 8; 9 10 11 12] B =
B =
2 4
1 2 3 4 2 5
5 6 7 8
9 10 11 12 >> C = A./B
C =
>> C = A.*B
C = 1 1
4 2
1 8 21 40
10 30 56 88
27 60 99 144
Matrix Operators
Operation MATLAB Comments
Form
Addition A + B Array addition is identical
Subtraction A - B Array subtraction is identical
Multiplication A * B Matrix multiplication of A and B. The number of
columns in A must equal the number of rows in B.
Division A / B Matrix division defined by A * inv(B), where
inv(B) is the inverse of matrix B.
Power A ^ B Matrix exponentiation of A and B. The power is
computed by repeated squaring
Examples
>> A = [ 2 4 ; 8 10]; >> A = [ 2 4 ; 8 10];
>> B = [2 4; 2 5]; >> B = [2 4; 2 5];
>> C = A*B >> C = B*A
C = C =
12 28 36 48
36 82 44 58
Common MATLAB
Functions
A few of the most common and useful MATLAB functions
who List current variables
whos List current variables, long form
clear Clear variables and functions from memory
disp Display matrix or text
clc Clear command window
demo Run demonstrations
Built-in Function of Elementary
Math
abs(x) Calculates |x|
angle(x) Returns the phase angle of the complex value x, in radians
exp(x) Calculate ex
mod(x) Remainder or modulo function
log(x) Calculates the natural logarithm logex
sqrt(x) Calculates the square root of x
sin(x) Calculates the sin(x), with x in radians
cos(x) Calculates the cos(x), with x in radians
tan(x) Calculates the tan(x), with x in radians
ceil(x) Rounds x to the nearest integer towards positive infinity
fix(x) Rounds x to the nearest integer towards zero
floor(x) Rounds x to the nearest integer towards minus infinity
round(x) Rounds x to the nearest integer
Examples
>> z = 2*sin(pi/2)+log(2) >> z = 2*sin(pi/2)+log(2)
z = z =
2.6931 2.6931
>> z = round(z) >> z = round(z)
z = z =
3 3