Sie sind auf Seite 1von 19

Data Analysis using S Language and MATLAB

A Brief Introduction of MATLAB


Mohammad Samsul Alam
Lecturer of Applied Statistics
Institute of Statistical Research and Training (ISRT)
University of Dhaka

Introduction to MATLAB

M. S. Alam,ISRT,DU

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 in


the late 1970s
MATLAB is an interactive software system for numerical

computations and graphics. It was designed to numerical


mathematics.

Introduction to MATLAB

M. S. Alam,ISRT,DU

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 the

command-line.
Low-level data manipulation utilities and numerical algorithms

with base load

Introduction to MATLAB

M. S. Alam,ISRT,DU

Variable Naming

The rules for variable naming in MATLAB can be summarized as

follows,
Variable names in MATLAB must start with a letter and can

have 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

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 allow

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

in 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

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

Mathematical Operation

Before in depth study, it is useful to look at the commands for

mathematical 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 a


as in asin, acos or atan).
Exponential functions exp, log, log10 and is to be used for ex ,
ln(x), log10 (x) and ab respectively.

Introduction to MATLAB

M. S. Alam,ISRT,DU

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

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

them are summarized in the Table 1.

Table: Other Mathematical Functions in MATLAB

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

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 represent

the 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

Vectors and Matrix in MATLAB

MATLAB allows data in both vector or matrix format. To enter

data in vector and matrix following general rule can be followed.


A row vector can be entered as object name = [values

separated by single space].


A column vector can be entered as object name = [values

separated by semi-colon].
A matrix can be entered as matrix name = [rows are to be

separated 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

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...

Table: Several Features of Vector and Matrix in MATLAB

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...

Table: Several Features of Vector and Matrix in MATLAB

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

The Table 5 shows essential commands for several matrix/vector

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

of a vector or matrix in MATLAB. The commands are given in


Tables 6 through Table 9
Table: Simple Descriptive Statistics in MATLAB

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

Table: Simple Descriptive Statistics in MATLAB

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...

Table: More Descriptive Statistics in MATLAB

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

Das könnte Ihnen auch gefallen