Beruflich Dokumente
Kultur Dokumente
Introduction to MATLAB
M. S. Alam,ISRT,DU
What is MATLAB
Introduction to MATLAB
M. S. Alam,ISRT,DU
General Structure
command-line.
Low-level data manipulation utilities and numerical algorithms
Introduction to MATLAB
M. S. Alam,ISRT,DU
Variable Naming
follows,
Variable names in MATLAB must start with a letter and can
Introduction to MATLAB
M. S. Alam,ISRT,DU
Some Basics
continuation.
Output is displayed unless the input line is terminated by a
semi-colon (;).
If one forgot to store an object in a variable, MATLAB will keep it
Introduction to MATLAB
M. S. Alam,ISRT,DU
Introduction to MATLAB
M. S. Alam,ISRT,DU
Mathematical Operation
mathematical operations
Addition, Subtraction, Multiplication and Division can be done
Introduction to MATLAB
M. S. Alam,ISRT,DU
Example
b = 3, c = 4 and d = 3,
a(b + c(c + d))a,
>> a = 2; b = 3; c = -4; d = -3;
>> a*(b+c*(c+d))*a;
Example: Use MATLAB to calculate the expression
a
b + b+a
ca
where a = 3, b = 5 and c = 3.
Introduction to MATLAB
M. S. Alam,ISRT,DU
Cont...
There are a variety of other functions in MATLAB. Some of
Command
round(x)
ceil(x)
floor(x)
fix(x)
rem(x,y)
mod(x,y)
abs(x)
sign(x)
factor(x)
Introduction to MATLAB
Description
Rounds a number to the nearest integer
Rounds a number up to the nearest integer
Rounds a number down to the nearest integer
Rounds a number to the nearest integer towards zero
The remainder left after division
The signed remainder left after division
The absolute value of x
The sign of x
The prime factors of x
M. S. Alam,ISRT,DU
following example,
>> s = sqrt(2);
>> format short; s
s =
1.4142
>> format long; s
s =
1.41421356237310
>> format rat; s
s =
1393/985
Introduction to MATLAB
M. S. Alam,ISRT,DU
separated by semi-colon].
A matrix can be entered as matrix name = [rows are to be
separated by semi-colon].
Introduction to MATLAB
M. S. Alam,ISRT,DU
Cont...
Consider an arbitrary vector v and matrix A,
Table: Several Features of Vector and Matrix in MATLAB
Command
v = [2 8 5]
v = [2; 8; 5]
A = [4 8 1; 6 1 9]
v(i)
A(i,j)
zeros(k,1)
zeros(1,k)
zeros(m,n)
eye(n)
diag(A)
Introduction to MATLAB
Description
To build a row vector
To build a column vector
To create a matrix
To see the ith element
To see the j th element of ith row
To build a column vector of length k
containing all zeros
To build a row vector of length k
containing all zeros
To build a m n matrix of zero
To build a n n identity matrix
To extract diagonal elements
M. S. Alam,ISRT,DU
Cont...
Command
[a1 a2]
[a1; a2]
A(:,j)
A(i,:)
A(:)
v(1:a)
v(a:end)
Introduction to MATLAB
Description
Glue two matrices a1 and a2 (of equal no. of rows)
side-by-side
Stack two matrices a1 and a2 (of equal no. of column)
on top of each other
To see column j of matrix A
To see row i of matrix A
To see all elements of matrix A
To see first to ath elements of vector v
To see ath element to last element of vector v
M. S. Alam,ISRT,DU
Cont...
Command
L = tril(A)
U = triu(A)
reshape(A,k,l)
linspace(a,n,b)
logspace(a,n,b)
Introduction to MATLAB
Description
Lower triangular portion of matrix A
upper triangular portion of matrix A
To reshape matrix A into k l matrix with
elements taking columnwise from A
To create a vector of n equally spaced
values between a and b inclusive
To create a vector of n logarithmically equally
spaced values between 10a and 10b inclusive
M. S. Alam,ISRT,DU
Matrix/Vector Computations
computations.
Table: Several Features of Vector and Matrix in MATLAB
Command
A*B
A.*B
A
inv(A)
A/B
A\B
Introduction to MATLAB
Description
Matrix multiplication AB
Element-by-element multiplication of A and B
Transpose of matrix A
Compute inverse of A
Compute AB 1
Compute A1 B
M. S. Alam,ISRT,DU
Descriptive Statistics
Simple descriptive statistics can be computed using the elements
Command
mean(v)
mean(A(:))
mean(A)
mean(A,2)
std(v)
std(v,1)
std(A(:))
std(A(:),1)
Introduction to MATLAB
Description
Compute mean of all elements in vector v
Compute mean of all elements in matrix A
To compute the column means of matrix A
To compute the row means of matrix A
Standard deviation of elements in vector v
Standard deviation of elements in vector v
normalize by 1
Standard deviation of all elements in matrix A
Standard deviation of all elements in matrix A
M. S. Alam,ISRT,DU
Descriptive Statistics
Command
std(A)
std(A,1)
std(A,0,2)
std(A,1,2)
var(v)
var(v,1)
var(A(:))
var(A(:),1)
Introduction to MATLAB
Description
Compute standard deviation of columns of matrix A
Compute standard deviation of columns of matrix A
normalizing by n
Compute standard deviation of rows of matrix A
Compute standard deviation of rows of matrix A
normalizing by n
Variance of elements in vector v
Variance of elements in vector v normalize by 1
Variance of all elements in matrix A
Variance of all elements in matrix A
M. S. Alam,ISRT,DU
Cont...
Command
var(A)
var(A,1)
var(A,0,2)
var(A,1,2)
cov(u,v)
cov(A)
corr(u,v)
Introduction to MATLAB
Description
Compute variance of columns of
matrix A
Compute variance of columns of
matrix A normalizing by n
Compute variance of rows of matrix A
Compute variance of rows of matrix A
normalizing by n
Gives the covariance between two variable u and v
Compute covariance matrix, giving the covariance
among the columns
Gives the correlation between two variable u and v
M. S. Alam,ISRT,DU
Cont...
Table: More Descriptive Statistics in MATLAB
Command
corr(u,v,type,kendall)
corr(u,v,type,spearman)
min(v)
max(v)
sum(v)
sum(A(:))
sum(A)
cumsum(A)
cumsum(A,2)
cumprod(A)
Introduction to MATLAB
Description
Gives the Kendalls tau statistic
for variable u and v
Gives the Spearmans rho correlation
statistic for variable u and v
Minimum value among the elements of v
Maximum value among the elements of v
Sum all the elements in vector v
Sum all the elements in matrix A
To compute the columns total
Cumulative sum of columns of A
Cumulative sum of rows of A
Cumulative product of columns of A
M. S. Alam,ISRT,DU