15
Introduction to MATLAB Computational Probability and Statistics 8/30/2017 (Temple University) Introduction to MATLAB 8/30/2017 1 / 15

Introduction to MATLAB - Temple Universitytug52596/AnaFiles/matlab_intro.pdf · Introduction to MATLAB Computational Probability and Statistics 8/30/2017 (Temple University) Introduction

Embed Size (px)

Citation preview

Page 1: Introduction to MATLAB - Temple Universitytug52596/AnaFiles/matlab_intro.pdf · Introduction to MATLAB Computational Probability and Statistics 8/30/2017 (Temple University) Introduction

Introduction to MATLABComputational Probability and Statistics

8/30/2017

(Temple University) Introduction to MATLAB 8/30/2017 1 / 15

Page 2: Introduction to MATLAB - Temple Universitytug52596/AnaFiles/matlab_intro.pdf · Introduction to MATLAB Computational Probability and Statistics 8/30/2017 (Temple University) Introduction

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

Page 3: Introduction to MATLAB - Temple Universitytug52596/AnaFiles/matlab_intro.pdf · Introduction to MATLAB Computational Probability and Statistics 8/30/2017 (Temple University) Introduction

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

Page 4: Introduction to MATLAB - Temple Universitytug52596/AnaFiles/matlab_intro.pdf · Introduction to MATLAB Computational Probability and Statistics 8/30/2017 (Temple University) Introduction

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

Page 5: Introduction to MATLAB - Temple Universitytug52596/AnaFiles/matlab_intro.pdf · Introduction to MATLAB Computational Probability and Statistics 8/30/2017 (Temple University) Introduction

(Temple University) Introduction to MATLAB 8/30/2017 5 / 15

Page 6: Introduction to MATLAB - Temple Universitytug52596/AnaFiles/matlab_intro.pdf · Introduction to MATLAB Computational Probability and Statistics 8/30/2017 (Temple University) Introduction

Built-in variables and constants

(Temple University) Introduction to MATLAB 8/30/2017 6 / 15

Page 7: Introduction to MATLAB - Temple Universitytug52596/AnaFiles/matlab_intro.pdf · Introduction to MATLAB Computational Probability and Statistics 8/30/2017 (Temple University) Introduction

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

Page 8: Introduction to MATLAB - Temple Universitytug52596/AnaFiles/matlab_intro.pdf · Introduction to MATLAB Computational Probability and Statistics 8/30/2017 (Temple University) Introduction

Elementary math functions

(Temple University) Introduction to MATLAB 8/30/2017 8 / 15

Page 9: Introduction to MATLAB - Temple Universitytug52596/AnaFiles/matlab_intro.pdf · Introduction to MATLAB Computational Probability and Statistics 8/30/2017 (Temple University) Introduction

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

Page 10: Introduction to MATLAB - Temple Universitytug52596/AnaFiles/matlab_intro.pdf · Introduction to MATLAB Computational Probability and Statistics 8/30/2017 (Temple University) Introduction

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

Page 11: Introduction to MATLAB - Temple Universitytug52596/AnaFiles/matlab_intro.pdf · Introduction to MATLAB Computational Probability and Statistics 8/30/2017 (Temple University) Introduction

Matrix functions

(Temple University) Introduction to MATLAB 8/30/2017 11 / 15

Page 12: Introduction to MATLAB - Temple Universitytug52596/AnaFiles/matlab_intro.pdf · Introduction to MATLAB Computational Probability and Statistics 8/30/2017 (Temple University) Introduction

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

Page 13: Introduction to MATLAB - Temple Universitytug52596/AnaFiles/matlab_intro.pdf · Introduction to MATLAB Computational Probability and Statistics 8/30/2017 (Temple University) Introduction

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

Page 14: Introduction to MATLAB - Temple Universitytug52596/AnaFiles/matlab_intro.pdf · Introduction to MATLAB Computational Probability and Statistics 8/30/2017 (Temple University) Introduction

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

Page 15: Introduction to MATLAB - Temple Universitytug52596/AnaFiles/matlab_intro.pdf · Introduction to MATLAB Computational Probability and Statistics 8/30/2017 (Temple University) Introduction

Control Flow

(Temple University) Introduction to MATLAB 8/30/2017 15 / 15