Title: Matlab Chapter 2: Array and Matrix Operations
1MatlabChapter 2 Array and Matrix Operations
2What is a vector?
- In Matlab, it is a single row (horizontal) or
column (vertical) of numbers or characters. - Vector is one row OR one column
- Does not have to have anything to do with
geometry - Number of components is not restricted
- Transpose of row is column, of column is row
3Vectors in Matlab
- Various forms for entering row and column
vectors
gtgt u1234 u 1 2 3 4
gtgt p1,2,3 p 1 2 3
gtgt v329 v 3 5 7 9
gtgt w47 w 4 5 6 7
4Transposing Vectors
gtgt u u 1 2 3 4 gtgt u' ans
1 2 3 4
- gtgt p
- p
- 1 2 3
- gtgt p'
- ans
- 1
- 2
- 3
5Augmenting Row Vectors
- gtgt p
- p
- 1 2 3
- gtgt v
- v
- 3 5 7 9
- gtgt zp v
- z
- 1 2 3 3 5 7 9
6Stacking Column Vectors
gtgt qut q 1 2 3 4
3 8 -4
gtgt t t 3 8 -4
7Functions to Generate Vectors
- linspace(a,b,c) produces c evenly spaced points
between a and b - gtgt linspace(2,4,5)
- ans
- 2.0000 2.5000 3.0000 3.5000
4.0000 - logspace(a,b,n) computes n pts linearly between a
and b, then uses them as exponents of 10 - gtgt logspace(1,3,4)
- ans
- 1.0e003
- 0.0100 0.0464 0.2154 1.0000
8Matrices
- A rectangular array of numbers or characters
- Rows numbered top to bottom
- Columns numbered left to right
- Size of array n x m
- number of rows is always stated first
- element of array x(k,j)
- row index is always stated firstkth row, jth col
- In general, indices can be negative or zero, but
not in MATLAB - Transpose of real matrix interchanges rows and
columns
9Transpose Concatenate Matrices
- gtgt A1,2,34,5,6
- A
- 1 2 3
- 4 5 6
- A is transpose
- gtgt A'
- ans
- 1 4
- 2 5
- 3 6
- gtgt B4,58,9
- B
- 4 5
- 8 9
- A,B augments (concatenates rows)
- ans
- 1 2 3 4 5
- 4 5 6 8 9
- AB stacks (concatenates cols)
- ans
- 1 4
- 2 5
- 3 6
- 4 5
- 8 9
10Vector Addressing
- v 1,7,3,8,6,7,3
- v(4) returns the 4th elementgtgt v(4)
- ans 8
- Note parentheses, not square brackets!
- v(25) returns elements 2 through 5
- gtgt v(25)
- ans 7 3 8 6
11Vector Addressing cont.
- v 1,7,3,8,6,7,3
- v() returns the entire vector as a column
- gtgt v()
- ans
- 1
- 7
- 3
- 8
- 6
- 7
- 3
12Matrix Addressing
- A
- 3 6 8
- 1 5 2
- A(2,1) returns the element in the 2nd row, 1st
column - ans 1
- A(,3) returns the 3rd column
- ans 8 2
- A(2,) returns the 2nd row
- ans 1 5 2
- A(12,23) returns all elements in the 1st and
2nd rows that are also in 2nd and 3rd columns
13Matrix Addressing
- A
- 3 6 8
- 1 5 2
- A(12,23) returns all elements in the 1st and
2nd rows that are also in 2nd and 3rd
columns ans 6 8 - 5 2
- Extract smaller array
- B A(12,1 3)B 3 8 1 2
14Arrays/Matrix Addressing
- Empty or null array
-
- A
- 3 6 8
- 1 5 2
- Remove a column
- A(,3) A 3 6 1 5
- Automatic enlargement
- A(3,5) 22A 3 6 0 0 0
- 1 5 0 0 0 0 0
0 0 22
15Arrays/Matrix Addressing
- Negative increment reverses order
- p3,8,4,6
- rev p(end-11)rev 6 4 8 3
- A(n) returns the nth element of matrix A, going
column by columnA 3 6 8 - 1 5 2
- A(4)ans 5
16Arrays/Matrix Addressing
- A() returns all elements as a column, going
column by column A 3 6 8
1 5 2 - A()ans 3 1 6 5
8 2
17Automatic growth of a vector
- If vector already defined as row or column,
assigning new element beyond current bounds ?
automatically grows to fit with intervening
elements set to 0 - x 12 x starts as 2-element column vector
- x(5) 24 x now has 5 elements. x(3) x(4)
0x - 1
- 2
- 0
- 0
- 24
18Automatic growth of a vector
- If variable is not yet vector, assigning element
beyond 1 defaults to creating a row vector - gtgtclear xx
- xx(4) 12 xx starts as a 4-element row vector
- xx
- 0 0 0 12
19Warning!
- Use clear to Avoid Errors
- Be careful
- suppose you simply want A to have u and v as
columns - A(,1) u
- A(,2) v
- A however, still has its previous columns!
- Use clear to avoid this
- clear A
20Vector and Matrix Functions
Play around with all these functions!
21Array Operations Same operation performed on
each corresponding element of array
- A
- 3 7 2
- 6 -1 5
- gtgt A3
- ans
- 6 10 5
- 9 2 8
- gtgt 2A
- ans
- 6 14 4
- 12 -2 10
- gtgt A.2
- ans
- 9 49 4
- 36 1 25
- B
- 4 6 1
- 3 9 2
- gtgt AB
- ans
- 7 13 3
- 9 8 7
- gtgt A.B
- ans
- 12 42 2
- 18 -9 10
- A./B
- ans
- 0.7500 1.1667 2.0000
- 2.0000 -0.1111 2.5000
22Math Functions Automatically Apply to Each Element
A 3 7 2 6 -1 5
gtgt sin(A) ans 0.1411 0.6570 0.9093
-0.2794 -0.8415 -0.9589
gtgt sqrt(A) ans 1.7321 2.6458
1.4142 2.4495
0 1.0000i 2.2361
23Vector Operations
v -4 5 2
gtgt dot(u,v) ans 8 gtgt cross(u,v) ans
-21 -26 23
gtgt norm(u) ans 6.1644 gtgt this is the
Euclidean length or magnitude
24Matrix Product
- C AB is defined if and only if the number of
columns in A equals the number of rows in B. Then
C will have the same number of rows as A and the
same number of columns as B - (3x2)(2x4) produces a 3x4
- (3x2)(3x2) not defined
- (3x2)(2x3) produces a 3x3
- (2x3)(3x2) produces a 2x2
25Matrix Product CAB
- The (i,j) entry in C is the sum of the products
of entries from the i-th row of A and the j-th
column of B
A 4 5 -1 3 2 0
B 3 2 1 5 1 -1 0
3 2 5 1 4
gtgt AB ans 15 -2 3 31 11
4 3 21
26Matrix Power
- An means multiply A by itself n times.
- Can only be done if A is square
- A.n means raise each element of A to the n-th
power - Can be done with any array A
27Matrix Division
- If A and B are matrices, A/B is NOT a defined
operation in linear algebra! - Matlab uses both right (/) and left (\) operator
symbols for special matrix operations we are not
covering in this course! - 1./A is an array operation creating a new array
with each entry being the reciprocal of the entry
in A - A./B is an array operation creating a new array
with each entry being the corresponding entry in
A divided by the corresponding entry in B
28Special Matrices
- eye(n)nxn identity matrix
- eye(3) 1 0 0
- 0 1 0
- 0 0 1
- ones(m,n) mxn matrix with all entries 1
- zeros(m,n)mxn matrix with all entries 0