Академический Документы
Профессиональный Документы
Культура Документы
Hasnain Rehman
Computer Programming Team
Contents
Overview
Start
Variables and data types Matrices Getting Help Summary
More Operations
Matlab
Overview
What is MATLAB??
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
Try the following exercises in the Command Window a) Type 2+3 after the prompt, followed by Enter
realmin
realmax
eps
format short
format long
format short e format long e
format short g
format long g
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]
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
Matrices
Some special matrices
Matrices
Some special matrices
Matrices
Some special matrices
Matrices
Some special matrices
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
1 1
1 1 A
1 1
0 0 B
Matrices
Creating a Matrix from Matrices
Matrices
Creating a Matrix from Matrices
1 1 0
1 1 0
1 1 0
Matrices
Creating a Matrix from Matrices
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:
>> 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
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.
Contents
Overview
Start
Variables and data types Matrices Getting Help Summary
More Operations
Summary
Colon ':', two meanings:
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
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)
s = v'*Q*v
More Operations
Vector Operations (1xm or nx1 matrix is vector) With x being a column vector
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
More Operations
Useful Vector Functions
min(v) max(v)
More Operations
Special Matrices
A = zeros(m,n)
B = ones(m,n)
I = eye(n)
D = diag([a b c]) Diagonal matrix of size 3 x 3 with a,b,c in the main diagonal
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...
Round to smallest integer not less than x Round to largest integer not greater than x Round towards nearest integer Round towards zero
More Operations
Random Matrices and Vectors
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?