Intro Matlab
Intro 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?
Getting Started
Starting MATLAB
MATLAB 7.0.1.lnk
Starting MATLAB
The Command Window
ans =
22-Sep-2007
MATLAB
MATLAB Basics
Variables and Arrays
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_vector2 = 1
5
1 7
5
7
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
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 =
>> 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 =
12 19
3 2
4 23
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
>> 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
A = C =
B =
7 7 7
Arithmetic Operations
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
12 28 36 48
36 82 44 58
Common MATLAB
Functions
A few of the most common and useful MATLAB functions
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 = z =
3 3