Vous êtes sur la page 1sur 56

Matlab Tutorial

Hasnain Rehman
Computer Programming Team

8th February, 2011

Contents
Overview

Start
Variables and data types Matrices Getting Help Summary

More Operations
Matlab

Overview
What is MATLAB??

MATLAB has become the language of technical computing

Overview
WHO USES MATLAB
Over one million people around the world use MATLAB for technical computing. They rely on MATLAB to help them to: Develop cancer therapies. Search for new sources of energy. Make our cars safer and more fuel-efficient. Explore outer space.

Contents
Overview

Start
Variables and data types Matrices Getting Help Summary

More operations

Start
MATLAB Desktop:

Start
MATLAB Desktop:

Start
Command Window type commands Workspace view program variables clear to clear double click on a variable to see it in the Array Editor Command History view past commands save a whole session using diary

Contents
Overview

Start
Variables and data types Matrices Getting Help Summary More operations

Variables and Data Types


Arirhmetics in MATLAB

Try the following exercises in the Command Window a) Type 2+3 after the prompt, followed by Enter

Variables and Data Types


Arirhmetics in MATLAB Try the following exercises in the Command Window
b) Next try the following:

Variables and Data Types


The line with the >> prompt is called the command
line.

How to use previous commands conveniently:


...

Smart recall: rst few characters +


2* +

Variables and Data Types


Assign values to Variables Try the following exercises in the Command Window
Assign values to variables to do arithmetical operations with the variables.

Variables and Data Types


Suppress output Try the following exercises in the Command Window
Now enter the statement ENDING WITH SEMICOLON (;)

The semicolon (;) prevents the value of b from being displayed.

Variables and Data Types


Arithmetic operations with Variables Try the following exercises in the Command Window

Assign values to variables x, y and z.

Variables and Data Types


Try the following exercises in the Command Window
3. Try the following:

Variables and Data Types


Numerical Precision Variables are stored as double precision numbers in IEEE floating point format.

realmin

Smallest positive floating point number: 2.23e-308

realmax
eps

Largest positive floating point number: 1.80e+308


Relative precision: 2.22e-16

Variables and Data Types


Control Display of Float Variables

format short

Fixed point format with 5 digits

format long
format short e format long e

Fixed point format with 15 digits


Floating point format, 5 digits Floating point format, 15 digits

format short g

Best of fixed or floating point with 5 digits (good choice)


Best of fixed or floating point with 15 digits

format long g

See help format for more information

Contents
Overview

Start
Variables and data types Matrices Getting Help Summary

More operations

Matrices
Creating a Matrix Try the following exercises in the Command Window
>> A = [8, 2, 1; 3, -1, 4; 7, 6, -5]

OR
>> A = [8 2 1; 3 -1 4; 7 6 -5]

To delimit columns, use comma or space To delimit rows, use semicolon

Matrices
Creating a Matrix Try the following exercises in the Command Window
>> A = [8, 2, 1; 3, -1, 4; 7, 6, -5]

OR
>> A = [8 A = 2 1; 3 -1 4; 7 6 -5]

8 3 7

2 -1 6

1 4 -5

Matrices
Some special matrices

Try the following exercises in the Command Window


>> eye(4) ans= 1 0 0 0 1 0 0 0 1 0 0 0

Matrices
Some special matrices

Try the following exercises in the Command Window


>> ans= 0 0 0 0 0 0 0 0 0 zeros(3)

Matrices
Some special matrices

Try the following exercises in the Command Window


>> ans= 0 0 0 0 0 0 0 0 zeros(2,4)

Matrices
Some special matrices

Try the following exercises in the Command Window


>> ans= 1 1 1 1 1 1 1 1 1 ones(3)

Matrices
Some special matrices

Try the following exercises in the Command Window


>> ans= 1 1 1 1 1 1 ones(3,2)

Matrices
Some special matrices: (Elementary matrices)

Ones matrix. Zeros matrix. Identity matrix. Pascals matrix. Magic matrix.

ones (m,n) zeros (m,n) eye (n) pascal (n) magic (n)

Others are gallery, hadamard, hankel, hilb, toeplitz, vander, etc. See help elmat.

Matrices
Creating a Matrix from Matrices

Try the following exercises in the Command Window Column-wise combination


>> A = ones(2,3); B = zeros(2,1);

1 1

1 1 A

1 1

0 0 B

Matrices
Creating a Matrix from Matrices

Try the following exercises in the Command Window Column-wise combination


>> A = ones(2,3); B = zeros(2,1); >> C = [A B] C = 1 1 1 1 1 1 0 0

Matrices
Creating a Matrix from Matrices

Try the following exercises in the Command Window Row-wise combination


>> A = ones(2,3); B = zeros(1,3);

1 1 0

1 1 0

1 1 0

Matrices
Creating a Matrix from Matrices

Try the following exercises in the Command Window Row-wise combination


>> A = ones(2,3); B = zeros(1,3); >> C = [A ; B] C = 1 1 0 1 1 0 1 1 0

Matrices
Indexing in MATLAB

Matrices
Indexing in MATLAB Try the following exercises in the Command Window

>> A = pascal(4)
A= 1 1 1 1

1
1 1

2
3 4

3
6 10

4
10 20

>> a = A(9)
a= 6

OR

>> a = A(3,3)

Matrices
Initializing vectors and colon operator:

Try the following exercises in the Command Window >> x = 1:10


elements are the integers 1,2,,10

>> x = 1:0.5:4
elements are values 1,1.5,2,,4

>> x = 10:-1:1
elements are values 10,9,,1 since the increment is -ve

Matrices
Get a multiple elements using indexing Try the following exercises in the Command Window
>> A = pascal(4)
A= 1 1 1 1 1 2 3 4 1 3 6 10 1 4 10 20

>> a = A(3,[2,3])
a = 2 3

Matrices
Get a ROW using indexing technique Try the following exercises in the Command Window
>> A = pascal(4)
A= 1 1 1 1 1 2 3 4 1 3 6 10 1 4 10 20

>> a = A(3,[1,2,3,4])
a = 1 3 6 10

Matrices
Get a row using colon operator Try the following exercises in the Command Window
>> A = pascal(4)
A= 1 1 1 1 1 2 3 4 1 3 6 10 1 4 10 20

>> a = A(3,: )
a = 1 3 6 10

Matrices
Get a COLUMN using colon operator
Try the following exercises in the Command Window >> A = pascal(4)
A= 1 1 1 1

1
1 1

2
3 4

3
6 10

4
10 20

>> a = A( : , 4 )
a = 1 4 10 20

Matrices
Delete a column
Try the following exercises in the Command Window

>> A = pascal(4)
A= 1 1 1 1 1 2 3 4 1 3 6 10 1 4 10 20

>> A( : , 4 ) = []
A= 1
1 1

1
2 3

1
3 6

10

Matrices
Add a column
Try the following exercises in the Command Window

>> A = pascal(4)
A= 1 1 1 1 1 2 3 4 1 3 6 10

>> A( : , 4 ) = [3;4;5;6]
A= 1
1 1

1
2 3

1
3 6

3
4 5

10

Matrices
Get a Submatrix from a Matrix
Try the following exercises in the Command Window

>>

A=
1 1 1 1 1 2 3 4 1 3 6 10 3 4 5 6

>> a = A(2:3,2:4)
a= 2 3 3 6 4 5

Contents
Overview

Start
Variables and data types Matrices Getting Help Summary

More Operations

Getting Help
To get help, type help or doc

To get help on a specific command (=built-in


function), type help command

Examples: help size, help plot, help figure,


help inv, ...

To get help on the help system, type help help

Getting Help
In the help text of Matlab functions, function names
and variables are in capital letters. Don't get confused! The (case-sensitive) naming convention specifies lowercase letters for built-in commands. It is just a way to highlight text.

Example: help round returns


ROUND Round towards nearest integer. ROUND(X) rounds the elements of X to the nearest integers. See also floor, ceil, fix. [...]

Contents
Overview

Start
Variables and data types Matrices Getting Help Summary

More Operations

Summary
Colon ':', two meanings:

Wildcard to select entire matrix row or column


A(3,:), B(:,5)

Defines a range in expressions like


indices = 1:5
steps = 1:3:61 t = 0:0.01:1
start increment

Returns row vector 1,2,3,4,5


Returns row vector 1,4,7,...,61 Returns vector 0,0.01,0.02,...,1
stop

Useful command to define ranges: linspace

Summary
Indexing Always "row before column"!

aij = A(i,j)
r = A(i,:) c = A(:,j)

Get an element
Get a row Get a column

B = A(i:k,j:l)

Get a submatrix

Useful indexing command end :


>> data = [4 -1 35 9 11 -2]; >> v = data(3:end) v = 35 9 11 -2

Contents
Overview

Start
Variables and data types Matrices Getting Help Summary

More Operations

More Operations
Matrix Operations

B = 3*A

Multiply by scalar

C = A*B + X - D
B = A' B = inv(A)

Add and multiply


Transpose A Invert A

s = v'*Q*v

Mix vectors and matrices


Determinant of A Eigenvalue decomposition Sing. value decomposition

d = det(A) [v lambda] = eig(A) [U S V] = svd(A) many many more...

More Operations
Vector Operations (1xm or nx1 matrix is vector) With x being a column vector

s = x'*x X = x*x' e = x*x

Inner product, result is a scalar


Outer product, result is a matrix Gives an error

Element-Wise Operations (for vectors/matrices)

s = x.+x

Element-wise addition

p = x.*x
q = x./x e = x.^3

Element-wise multiplication
Element-wise division Element-wise power operator

More Operations
Useful Vector Functions

sum(v) cumsum(v) prod(v) cumprod(v) diff(v) mean(v) std(v)

Compute sum of elements of v

Compute cumulative sum of elements of v


Compute product of elements of v

Compute cumulative product of elements of v


Compute difference of subsequent elements [v(2)-v(1) v(3)-v(2) ...] Mean value of elements in v Standard deviation of elements

More Operations
Useful Vector Functions

min(v) max(v)

Return smallest element in v

Return largest element in v

sort(v,'ascend') Sort in ascending order sort(v,'descend') Sort in descending order find(v)


Return vector of indices of all nonzero elements in v. Great in combination with vectorized conditions. Example: ivec = find(datavec == 5).

More Operations
Special Matrices

A = zeros(m,n)

Zero matrix of size m x n

B = ones(m,n)
I = eye(n)

Matrix of size m x n with all 1's


Identity matrix of size n

D = diag([a b c]) Diagonal matrix of size 3 x 3 with a,b,c in the main diagonal

Just for fun

M = magic(n)

Magic square matrix of size n x n. (All rows and columns sum up to the same number)

More Operations
Talking about Float Variables...

ceil(x) floor(x) round(x) fix(x)

Round to smallest integer not less than x Round to largest integer not greater than x Round towards nearest integer Round towards zero

If x is a matrix, the functions are applied to each element of x.

More Operations
Random Matrices and Vectors

R = rand(m,n) N = randn(m,n) v = randperm(n)

Matrix with m x n uniformly distributed random numbers from interval [0..1] Row vector with m x n normally distributed random numbers with zero mean, unit variance Row vector with a random permutation of the numbers 1 to n

QUESTIONS?

Vous aimerez peut-être aussi