1 Week 3: Vectors and Matrices (Part III) READING: 2.2 – 2.4 EECS 1541 -- Introduction to...
-
Upload
rosanna-oliver -
Category
Documents
-
view
215 -
download
1
Transcript of 1 Week 3: Vectors and Matrices (Part III) READING: 2.2 – 2.4 EECS 1541 -- Introduction to...
1
EECS 1541 -- Introduction to Computing for the Physical Sciences
Vectors and Matrices as Function Arguments
2
EECS 1541 -- Introduction to Computing for the Physical Sciences
• In MATLAB, an entire vector or matrix can be passed as an argument to a function such as sum, prod
>> v = 1:5;>> sum(v)
ans =
15
• For a vector, the function will be evaluated on every element.
• Example:
>> v = 1:5;>> prod(v)
ans =
120
Means 1 + 2 + 3 + 4 + 5
Means 1 * 2 * 3 * 4 * 5
Vectors and Matrices as Function Arguments
3
EECS 1541 -- Introduction to Computing for the Physical Sciences
• For matrices, the sum and prod functions operate on every individual column.
• Hence, if a matrix has dimensions: r x c, the result for the sum and prod functions will be a 1 x c row vector
>> A = [1:3; 2:4; 5:7];>> sum(A)
ans =
8 11 14
• Example:
from 3 + 4 + 7
from 2 + 3 + 6
from 1 + 2 + 5
Vectors and Matrices as Function Arguments
4
EECS 1541 -- Introduction to Computing for the Physical Sciences
>> A = [1:3; 2:4; 5:7];>>
• Example: How would you compute the sum of all the entries in A?
Vectors and Matrices as Function Arguments
5
EECS 1541 -- Introduction to Computing for the Physical Sciences
• Some other commonly used functions in array include:
functions descriptions Examples in vectors:
min Returns the smallest number
>> v = [1 2 3 4];>> min(v)ans = 1
max Returns the largest number
>> v = [1 2 3 4];>> max(v)ans = 4
cumsum Returns the cumulative sum
>> v = [1 2 3 4];>> cumsum(v)ans = 1 3 6 10
cumprod Returns the cumulative product
>> v = [1 2 3 4];>> cumprod(v)ans = 1 2 6 24
Vectors and Matrices as Function Arguments
6
EECS 1541 -- Introduction to Computing for the Physical Sciences
>> A = [1:3; 2:-1:0; 5:7];
• Example: How would you determine the largest number in the following matrix?
Vectors and Matrices as Function Arguments
7
EECS 1541 -- Introduction to Computing for the Physical Sciences
• Example: >> A = [1:3; 2:4; 5:7];>> cumsum(A)
ans =
1 2 3 3 5 7 8 11 14
• Hence, the resulting matrix will have the same dimension as the input matrix.
• When the cumsum and cumprod functions are used in matrices, they return the cumulative sum or product of every column:
Matrix transpose
8
EECS 1541 -- Introduction to Computing for the Physical Sciences
• Example: >> A = [1 2; 3 4; 5 6]
A =
>> transpose_A = A’
transpose_A =
1 3 5 2 4 6
• If A is an m x n matrix, then the transpose of A is an n x m matrix, where the row vectors of A are written as column vectors
1 2 3 4 5 6
Scalar multiplication
9
EECS 1541 -- Introduction to Computing for the Physical Sciences
• Example of a vector: >> v = [1 3 5 7];
>> v*3
ans = 3 9 15 21
• Numerical or arithmetic operations can be performed on every element in the entire vectors or matrices
Scalar multiplication
10
EECS 1541 -- Introduction to Computing for the Physical Sciences
• Example of a matrix: >> A = [1 3; 5 7; 2 4];
>> A*3
ans = 3 9 15 21 6 12
Scalar addition and subtraction
11
EECS 1541 -- Introduction to Computing for the Physical Sciences
• Example: >> A = [1 3; 5 7; 2 4];
>> A + 2
ans = 3 5 7 9 4 6
Add every element by 2
Array operations: addition and subtraction
12
EECS 1541 -- Introduction to Computing for the Physical Sciences
• You can perform element-by-element arithmetic with two arrays of the same size
>> v1 = [1 2 3];>> v2 = [4 5 6];>> v1 + v2
ans = 5 7 9
• Example:
Adding two vectors
Array operations: addition and subtraction
13
EECS 1541 -- Introduction to Computing for the Physical Sciences
>> A1 = [1 2 3; 2 2 5];>> A2 = [1 0 1; 3 6 1];
>> A1 + A2
ans = 2 2 4 5 8 6
• Example:
Adding two matrices
Array operations: addition and subtraction
14
EECS 1541 -- Introduction to Computing for the Physical Sciences
>> A1 = [1 2 3; 2 2 5];>> A2 = [1 0 1; 3 6 1];
>> A1 - A2
ans = 0 2 2 -1 -4 4
• Example:
Subtracting two matrices
Array operations: addition and subtraction
15
EECS 1541 -- Introduction to Computing for the Physical Sciences
>> A1 = [1 2 3; 2 2 5];>> A2 = [1 0 1; 3 6 1];>> A2(2,:)=[]
>> A1 - A2
ans = Error using - Matrix dimensions must agree.
• Example:
Subtracting two matrices with different dimensions
Matrix Multiplication
16
EECS 1541 -- Introduction to Computing for the Physical Sciences
• Multiplication between two matrices works as follows:
• To multiply a matrix A by a matrix B to result in a matrix C, the number of columns of A must equal to the number of row in B
mxpnxpmxn CBA
14
6
212
24
1
4*
23
21• Example:
2 x 2 2 x 1 2 x 1
the inner dimensions must be the same
Matrix Multiplication
17
EECS 1541 -- Introduction to Computing for the Physical Sciences
• Example:
>> A = [1 2; 3 2];>> B = [4 1]’;
>> A*B
ans = 6 14
Multiplying two matrices
Vector Multiplication
18
EECS 1541 -- Introduction to Computing for the Physical Sciences
1111 xnxcxnr svv
6
1
0
2
*421
• Example:
1 x 3 3 x 1 1 x 1
nxnxnrnxc svv 11
Vector Multiplication
19
EECS 1541 -- Introduction to Computing for the Physical Sciences
• Example:
>> A = [1 2 4];
>> B = [2; 0; 1];
>> A*B
ans = 6
Multiplying two vectors
1 x 3 vector
3 x 1 vector
Vector Multiplication
20
EECS 1541 -- Introduction to Computing for the Physical Sciences
• Example:
>> A = [1 2 4];
>> B = [2; 0; 1];
>> B*A
ans = 2 4 8 0 0 0 1 2 4
1 x 3 vector
3 x 1 vector
Multiplying two vectors
3 x 3 matrix
Array operations: Multiplication
21
EECS 1541 -- Introduction to Computing for the Physical Sciences
• How do we perform element-by-element multiplication or division between 2 vectors or 2 matrices?
• Example: >> v1 = [1:6]
v1 = 1 2 3 4 5 6
>> v1*v1
MATLAB will interpret this one as multiplying a 1 x 6 vector with a 1 x 6 vector
>> Error using * Inner matrix dimensions must agree.
Array operations: Multiplication
22
EECS 1541 -- Introduction to Computing for the Physical Sciences
• To perform element-by-element multiplication-based operation between multiple vectors, a “dot” must be placed in front of the operator
operators descriptions Examples in vectors:
.* element-by-element multiplication
>> v = [1 2 3 4];>> v.*vans = 1 4 9 16
./ element-by-element division
>> v = [1 2 3 4];>> v./vans = 1 1 1 1
.^ element-by-element exponentiation
>> v = [1 2 3 4];>> v.^3ans = 1 8 27 64
Array operations: Multiplication
23
EECS 1541 -- Introduction to Computing for the Physical Sciences
• Recall from LAB 2, when we plot a function that involves multiplication among sub-functions, we are performing element-by-element multiplication operation between multiple vectors
• Example, to plot the function: y = xex
>> x = [0:0.1:0.5]
x = 0.0 0.1 0.2 0.3 0.4 0.5
>> exp(x)
ans = 1.000 1.1052 1.2214 1.3499 1.4918 1.6487
>> y = x.*exp(x)
y = 0.000 0.1105 0.24438 0.4050 0.5967 0.8244
A row vector for the independent variable
Array operations: Multiplication
24
EECS 1541 -- Introduction to Computing for the Physical Sciences
0 0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4 0.45 0.50
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
Array operations: Multiplication
25
EECS 1541 -- Introduction to Computing for the Physical Sciences
• To perform element-by-element multiplication-based operation between 2 matrices, the dimensions must be the same
>> A = [1 2; 3 2];>> B = [2 0; 1 4];
>> A.*B
ans = 2 0 3 8
23
21A
41
02B
Multiply each element in the same position of A and B
4*21*3
0*22*1Final result is:
Array operations: Multiplication
26
EECS 1541 -- Introduction to Computing for the Physical Sciences
• NOTE: the difference between element-by-element multiplication and the actual matrix multiplication in this example since we have two 2 x 2 matrices
>> A = [1 2; 3 2];>> B = [2 0; 1 4];
>> A.*B
ans = 2 0 3 8
>> A = [1 2; 3 2];>> B = [2 0; 1 4];
>> A*B
ans = 4 8 8 8
Matrix multiplicationElement-by-element multiplication
Array operations: Multiplication
27
EECS 1541 -- Introduction to Computing for the Physical Sciences
>> A = [1 2; 3 2];>> B = [2 0; 1 4];
>> B.*A
ans = 2 0 3 8
>> A = [1 2; 3 2];>> B = [2 0; 1 4];
>> B*A
ans = 2 4 13 10
Matrix multiplicationElement-by-element multiplication
A.*B = B.*A A*B ≠ B*A
Array operations: Multiplication
28
EECS 1541 -- Introduction to Computing for the Physical Sciences
• Example: >> v = [2:2:8];>> v.^2/2
ans = 2 8 18 32
• Example: >> A = [ones(1,3); 1:3];>> A.^2’
ans =
1 1 1 41 9
^ has higher precedence than /