Title: MATLAB Basics
1MATLAB Basics
- With a brief review of linear algebra
- by Lanyi Xu
- modified by D.G.E. Robertson
21. Introduction to vectors and matrices
- MATLAB MATrix LABoratory
- What is a Vector?
- What is a Matrix?
- Vector and Matrix in Matlab
3What is a vector
- A vector is an array of elements, arranged in
column, e.g.,
X is a n-dimensional column vector.
4- In physical world, a vector is normally
3-dimensional in 3-D space or 2-dimensional in a
plane (2-D space), e.g.,
, or
5- If a vector has only one dimension, it becomes a
scalar, e.g.,
6Vector addition
- Addition of two vectors is defined by
Vector subtraction is defined in a similar
manner. In both vector addition and subtraction,
x and y must have the same dimensions.
7Scalar multiplication
- A vector may be multiplied by a scalar, k,
yielding
8Vector transpose
- The transpose of a vector is defined, such that,
if x is the column vector
its transpose is the row vector
9Inner product of vectors
- The quantity xTy is referred as the inner product
or dot product of x and y and yields a scalar
value (or x y).
If xTy 0 x and y are said to be orthogonal.
10- In addition, xTx , the squared length of the
vector x , is - The length or norm of vector x is denoted by
11Outer product of vectors
- The quantity of xyT is referred as the outer
product and yields the matrix
12- Similarly, we can form the matrix xxT as
where xxT is called the scatter matrix of vector
x.
13Matrix operations
- A matrix is an m by n rectangular array of
elements in m rows and n columns, and normally
designated by a capital letter. The matrix A,
consisting of m rows and n columns, is denoted as -
14Where aij is the element in the ith row and jth
column, for i1,2,?,m and j1,2,,n. If m2 and
n3, A is a 2?3 matrix
15- Note that vector may be thought of as a special
case of matrix - a column vector may be thought of as a matrix of
m rows and 1 column - a rows vector may be thought of as a matrix of 1
row and n columns - A scalar may be thought of as a matrix of 1 row
and 1 column.
16Matrix addition
- Matrix addition is defined only when the two
matrices to be added are of identical dimensions,
i.e., that have the same number of rows and
columns.
e.g.,
17 18Scalar multiplication
- The matrix A may be multiplied by a scalar k.
Such multiplication is denoted by kA where
i.e., when a scalar multiplies a matrix, it
multiplies each of the elements of the matrix,
e.g.,
19 20Matrix multiplication
- The product of two matrices, AB, read A times B,
in that order, is defined by the matrix
21The product AB is defined only when A and B are
comfortable, that is, the number of columns is
equal to the number of rows in B. Where A is m?p
and B is p?n, the product matrix cij has m rows
and n columns, i.e.,
22For example, if A is a 2?3 matrix and B is a 3?2
matrix, then AB yields a 2?2 matrix, i.e.,
In general,
23For example, if
and
, then
24and
Obviously,
.
25Vector-matrix Product
- If a vector x and a matrix A are conformable, the
product yAx is defined such that
26For example, if A is as before and x is as follow,
, then
27Transpose of a matrix
- The transpose of a matrix is obtained by
interchanging its rows and columns, e.g., if
Or, in general, Aaij, ATaji.
then
28Thus, an m?n matrix has an n?m transpose. For
matrices A and B, of appropriate dimension, it
can be shown that
29Inverse of a matrix
- In considering the inverse of a matrix, we must
restrict our discussion to square matrices. If A
is a square matrix, its inverse is denoted by A-1
such that
where I is an identity matrix.
30An identity matrix is a square matrix with 1
located in each position of the main diagonal of
the matrix and 0s elsewhere, i.e.,
31It can be shown that
32MATLAB basic operations
- MATLAB is based on matrix/vector mathematics
- Entering matrices
- Enter an explicit list of elements
- Load matrices from external data files
- Generate matrices using built-in functions
- Create vectors with the colon () operator
33gtgt x1 2 3 4 5 gtgt A 16 3 2 13 5 10 11 8 9
6 7 12 4 15 14 1 A 16 3 2 13
5 10 11 8 9 6 7 12
4 15 14 1 gtgt
34(No Transcript)
35(No Transcript)
36Generate matrices using built-in functions
- Functions such as zeros(), ones(), eye(),
magic(), etc.
gtgt Azeros(3) A 0 0 0 0 0
0 0 0 0 gtgt Bones(3,2) B 1
1 1 1 1 1
37gtgt Ieye(4) (i.e., identity matrix) I 1 0
0 0 0 1 0 0 0 0 1
0 0 0 0 1 gtgt Amagic(4) (i.e.,
magic square) A 16 2 3 13 5
11 10 8 9 7 6 12 4 14
15 1 gtgt
38Generate Vectors with Colon () Operator
The colon operator uses the following rules to
create regularly spaced vectors jk is the
same as j,j1,...,k jk is empty if j gt
k jik is the same as j,ji,j2i, ...,k jik
is empty if i gt 0 and j gt k or if i lt 0 and j lt
k where i, j, and k are all scalars.
39Examples
gtgt c05 c 0 1 2 3 4
5 gtgt b00.21 b 0 0.2000 0.4000
0.6000 0.8000 1.0000 gtgt d8-13 d 8
7 6 5 4 3 gtgt e82 e
Empty matrix 1-by-0
40Basic Permutation of Matrix in MATLAB
- sum, transpose, and diag
- Summation
- We can use sum() function.
- Examples,
gtgt Xones(1,5) X 1 1 1 1
1 gtgt sum(X) ans 5 gtgt
41gtgt Amagic(4) A 16 2 3 13 5
11 10 8 9 7 6 12 4
14 15 1 gtgt sum(A) ans 34 34
34 34 gtgt
42Transpose
gtgt Amagic(4) A 16 2 3 13
5 11 10 8 9 7 6 12
4 14 15 1 gtgt A' ans 16 5
9 4 2 11 7 14 3 10
6 15 13 8 12 1 gtgt
43Expressions of MATLAB
44Operators
Addition-Subtraction Multiplication / Division
\ Left division Power ' Complex conjugate
transpose ( ) Specify evaluation order
45Functions
MATLAB provides a large number of standard
elementary mathematical functions, including
abs, sqrt, exp, and sin.
Useful constants
pi 3.14159265... i Imaginary unit (
) j Same as i
46gtgt rho(1sqrt(5))/2 rho 1.6180 gtgt
aabs(34i) a 5 gtgt
47Basic Plotting Functions plot( )
The plot function has different forms, depending
on the input arguments. If y is a vector,
plot(y) produces a piecewise linear graph of
the elements of y versus the index of the
elements of y. If you specify two vectors as
arguments, plot(x,y) produces a graph of y versus
x.
48Example, x 0pi/1002pi y sin(x) plot(x,y)
49Multiple Data Sets in One Graph
x 0pi/1002pi y sin(x) y2
sin(x-.25) y3 sin(x-.5) plot(x,y,x,y2,x,y3)
50Distance between a Line and a Point
- given line defined by points a and b find the
perpendicular distance (d) to point c - d
- norm(cross((b-a),(c-a)))/norm(b-a)