Upload
abrar-ahmed
View
219
Download
0
Embed Size (px)
Citation preview
8/17/2019 Lecture 14 Mat Lab
1/19
Data Analysis using S Language and MATLAB
A Brief Introduction of MATLAB
Mohammad Samsul Alam
Lecturer of Applied StatisticsInstitute of Statistical Research and Training (ISRT)
University of Dhaka
Introduction to MATLAB M. S. Alam,ISRT,DU
http://find/http://goback/
8/17/2019 Lecture 14 Mat Lab
2/19
What is MATLAB
• The term MATLAB stands for Matrix Laboratory
• Cleve Moler, the chairman of the computer science department at
the University of New Mexico, started developing MATLAB inthe late 1970s
• MATLAB is an interactive software system for numericalcomputations and graphics. It was designed to numericalmathematics.
Introduction to MATLAB M. S. Alam,ISRT,DU
http://find/
8/17/2019 Lecture 14 Mat Lab
3/19
General Structure
• MATLAB is very similar to R in structure
• When opened a basic command-line interface is presented
• Everything can be (and probably should be) done from thecommand-line.
• Low-level data manipulation utilities and numerical algorithmswith base load
Introduction to MATLAB M. S. Alam,ISRT,DU
http://find/
8/17/2019 Lecture 14 Mat Lab
4/19
Variable Naming
• The rules for variable naming in MATLAB can be summarized asfollows,
• Variable names in MATLAB must start with a letter and canhave maximum 31 characters. The trailing characters can be
numbers, letters or underscore.• Variable names in MATLAB are case sensitive. So, a and A are
different objects.• Variable names should not coincide with a predefined MATLAB
command or any user-defined subroutines.
• To check the validity of variable name one case use isvarname
command as the following way
isvarname variable_name
Introduction to MATLAB M. S. Alam,ISRT,DU
http://find/
8/17/2019 Lecture 14 Mat Lab
5/19
Some Basics
• The percent sign (%) is to be placed before writing any comment.
• Use single quotes, not double quotes.
• Use three periods (. . .) at the end of the command line to allowcontinuation.
• Output is displayed unless the input line is terminated by asemi-colon (;).
• If one forgot to store an object in a variable, MATLAB will keep itin a variable named as ans until the next statement is executed.
•
Help for any command is available through the help (text),helpwin (separate window), doc (browser) or lookfor (apropos)commands.
Introduction to MATLAB M. S. Alam,ISRT,DU
http://find/
8/17/2019 Lecture 14 Mat Lab
6/19
Scaler and Quantities
• It is better to start with the basic ideas of equation and variables.Consider the following two commands,
>> a = 3
a =
3
> > b = 4 ;
Introduction to MATLAB M. S. Alam,ISRT,DU
http://find/
8/17/2019 Lecture 14 Mat Lab
7/19
Mathematical Operation
• Before in depth study, it is useful to look at the commands formathematical operations
• Addition, Subtraction, Multiplication and Division can be done
by +,-,* and / respectively.• Trigonometric functions sin (sine), cos (cosine) and tan
(tangent) (with their inverses being obtained by appending an aas in asin, acos or atan).
• Exponential functions exp, log, log10 and is to be used for ex,ln(x),log10(x) and a
b respectively.
Introduction to MATLAB M. S. Alam,ISRT,DU
http://find/
8/17/2019 Lecture 14 Mat Lab
8/19
Example
• Determine the value of the following expression when a = 2,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
b− a
b + b+aca
where a = 3, b = 5 and c = −3.
Introduction to MATLAB M. S. Alam,ISRT,DU
http://find/
8/17/2019 Lecture 14 Mat Lab
9/19
Cont...
• There are a variety of other functions in MATLAB. Some of
them are summarized in the Table 1.
Table: Other Mathematical Functions in MATLAB
Command Descriptionround(x) Rounds a number to the nearest integerceil(x) Rounds a number up to the nearest integerfloor(x) Rounds a number down to the nearest integerfix(x) Rounds a number to the nearest integer towards zero
rem(x,y) The remainder left after division mod(x,y) The signed remainder left after divisionabs(x) The absolute value of xsign(x) The sign of xfactor(x) The prime factors of x
Introduction to MATLAB M. S. Alam,ISRT,DU
http://find/
8/17/2019 Lecture 14 Mat Lab
10/19
Format: The way in which numbers appear in MATLAB
• A number in MATLAB can be displayed in three different formats•
These are short
(5 digits), long
(15 digits) and rat
(try to representthe answer as a rational)• Difference among these three types can be illustrated by the
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
http://find/
8/17/2019 Lecture 14 Mat Lab
11/19
Vectors and Matrix in MATLAB
• MATLAB allows data in both vector or matrix format. To enterdata in vector and matrix following general rule can be followed.
• A row vector can be entered as object name = [valuesseparated by single space].
• A column vector can be entered as object name = [valuesseparated by semi-colon].
• A matrix can be entered as matrix name = [rows are to beseparated by semi-colon].
Several features of vector and matrix can be extracted in
MATLAB. Some of them are shown in Tables 2, 3 and 4.
Introduction to MATLAB M. S. Alam,ISRT,DU
http://find/
8/17/2019 Lecture 14 Mat Lab
12/19
Cont...
• Consider an arbitrary vector v and matrix A,
Table: Several Features of Vector and Matrix in MATLAB
Command Descriptionv = [ 2 8 5 ] To build a row vectorv = [2; 8; 5] To build a column vector
A = [ 4 8 1 ; 6 1 9 ] To create a matrixv(i) To see the ith elementA(i,j) To see the jth element of ith rowzeros(k,1) To build a column vector of length k
containing all zeros
zeros(1,k) To build a row vector of length kcontaining all zeros
zeros(m,n) To build a m× n matrix of zeroeye(n) To build a n× n identity matrixdiag(A) To extract diagonal elements
Introduction to MATLAB M. S. Alam,ISRT,DU
http://find/http://goback/
8/17/2019 Lecture 14 Mat Lab
13/19
Cont...
Table: Several Features of Vector and Matrix in MATLAB
Command Description[a1 a2] Glue two matrices a1 and a2 (of equal no. of rows)
side-by-side[a1; a2] Stack two matrices a1 and a2 (of equal no. of column)
on top of each otherA(:,j) To see column j of matrix AA(i,:) To see row i of matrix A
A(:) To see all elements of matrix Av(1:a) To see first to ath elements of vector vv(a:end) To see ath element to last element of vector v
Introduction to MATLAB M. S. Alam,ISRT,DU
http://find/http://goback/
8/17/2019 Lecture 14 Mat Lab
14/19
Cont...
Table: Several Features of Vector and Matrix in MATLAB
Command DescriptionL = tril(A) Lower triangular portion of matrix A
U = triu(A) upper triangular portion of matrix Areshape(A,k,l) To reshape matrix A into k × l matrix with
elements taking columnwise from Alinspace(a,n,b) To create a vector of n equally spaced
values between a and b inclusive
logspace(a,n,b) To create a vector of n logarithmically equallyspaced values between 10a and 10b inclusive
Introduction to MATLAB M. S. Alam,ISRT,DU
http://find/
8/17/2019 Lecture 14 Mat Lab
15/19
Matrix/Vector Computations
• The Table 5 shows essential commands for several matrix/vectorcomputations.
Table: Several Features of Vector and Matrix in MATLAB
Command DescriptionA*B Matrix multiplication ABA.*B Element-by-element multiplication of A and BA’ Transpose of matrix Ainv(A) Compute inverse of A
A/B Compute AB−1
A \B Compute A−1B
Introduction to MATLAB M. S. Alam,ISRT,DU
http://find/
8/17/2019 Lecture 14 Mat Lab
16/19
8/17/2019 Lecture 14 Mat Lab
17/19
Descriptive Statistics
Table: Simple Descriptive Statistics in MATLAB
Command Descriptionstd(A) Compute standard deviation of columns of matrix Astd(A,1) Compute standard deviation of columns of matrix A
normalizing by nstd(A,0,2) Compute standard deviation of rows of matrix Astd(A,1,2) Compute standard deviation of rows of matrix A
normalizing by n
var(v) Variance of elements in vector vvar(v,1) Variance of elements in vector v normalize by 1var(A(:)) Variance of all elements in matrix Avar(A(:),1) Variance of all elements in matrix A
Introduction to MATLAB M. S. Alam,ISRT,DU
http://find/
8/17/2019 Lecture 14 Mat Lab
18/19
8/17/2019 Lecture 14 Mat Lab
19/19
Cont...
Table: More Descriptive Statistics in MATLAB
Command Descriptioncorr(u,v,’type’,’kendall’) Gives the Kendall’s tau statistic
for variable u and vcorr(u,v,’type’,’spearman’) Gives the Spearman’s rho correlation
statistic for variable u and v min(v) Minimum value among the elements of v max(v) Maximum value among the elements of vsum(v) Sum all the elements in vector vsum(A(:)) Sum all the elements in matrix A
sum(A) To compute the columns totalcumsum(A) Cumulative sum of columns of Acumsum(A,2) Cumulative sum of rows of Acumprod(A) Cumulative product of columns of A
Introduction to MATLAB M. S. Alam,ISRT,DU
http://find/