Upload
lekhuong
View
224
Download
0
Embed Size (px)
Citation preview
Introduction to MATLABComputational Probability and Statistics
8/30/2017
(Temple University) Introduction to MATLAB 8/30/2017 1 / 15
Course and labs web page
Course web page (Dr Pei Wang’s students):cis-linux1.temple.edu/~wangp/2033-PL/2033-index.htm
Labs web page:astro.temple.edu/~tug52596/cis2033fall2017
(Temple University) Introduction to MATLAB 8/30/2017 2 / 15
MATLAB licence at Temple
All Temple University in-campus computers have student-versionMATLAB installed.
Temple University also provides student licenses:download.temple.edu
(Temple University) Introduction to MATLAB 8/30/2017 3 / 15
About MATLAB
MATLAB (MATrix LABoratory) can be used for:
Numerical Computation (Technical computing)VisualizationApplication Development
Everything is represented as a matrix.
(Temple University) Introduction to MATLAB 8/30/2017 4 / 15
(Temple University) Introduction to MATLAB 8/30/2017 5 / 15
Built-in variables and constants
(Temple University) Introduction to MATLAB 8/30/2017 6 / 15
Numeric Types
Matlab provides wide range of numerical and floating point types such as:
double,int (8,16,32 and 64 bits),uint ( 8,16,32,64 bits).
(Temple University) Introduction to MATLAB 8/30/2017 7 / 15
Elementary math functions
(Temple University) Introduction to MATLAB 8/30/2017 8 / 15
Matrices and vectors
To enter a matrix:2 5 36 4 1typeA = [2, 5, 3; 6, 4, 1].
Another way to specify a row:B = [1:1.5:6; 2 3 4 5].
Add elements to a matrix:D =[]; D=[D;5]; D=[D;6;7]
Pre-defined matrices:E = zeros(4, 5)
(Temple University) Introduction to MATLAB 8/30/2017 9 / 15
Basic matrix operations:
Addition:C = A + B
Subtraction:D = A - B
Multiplication:E = A * B (Matrix multiplication)E = A .* B (Element wise multiplication, A and B same size)
Division:Left Division and Right DivisionF = A . / B (Element wise division)F = A / B = A*inv(B) (A * inverse of B)F = A .\B (Element wise division)F = A\B=inv(A)*B (inverse of A * B)
(Temple University) Introduction to MATLAB 8/30/2017 10 / 15
Matrix functions
(Temple University) Introduction to MATLAB 8/30/2017 11 / 15
Functions that return matrices
Matrix with ZEROS:A = zeros(m, n)
Matrix with ONES:B = ones(m, n)
IDENTITY Matrix:I = eye(m, n)
RANDOM Matrix:I = rand(m, n)
m - rows,
n - columns.
(Temple University) Introduction to MATLAB 8/30/2017 12 / 15
Array indexing, obtaining information
Size(A): returns [m n]
Length(A): returns max(size(A))
B = A(2:4,3:5) - B is a subset of A, containing rows 2 to 4 andcolumns 3 to 5.
D = A(:, [1,15,20]) - all rows, specified colums of matrix A
A(:, 2)=[] - delete second column.
(Temple University) Introduction to MATLAB 8/30/2017 13 / 15
Characters and Strings
A = ’Alice’ - single quotes
use two single quotes within the definition if the text includes a singlequote
Concatenation: A = [’Alice’, ’followed’, ’the’, ’rabbit.’]
Convert numeric values to strings
num2str ,int2str .
(Temple University) Introduction to MATLAB 8/30/2017 14 / 15
Control Flow
(Temple University) Introduction to MATLAB 8/30/2017 15 / 15