Day05 and 06 Vectors and Matrices 2023 StudentVersion v4
Day05 and 06 Vectors and Matrices 2023 StudentVersion v4
1
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
Reference
} You should be following along in Sections 1.6 and 5.2
of the Attaway book.
3 Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
1
9/19/23
Lab C
4
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
Arrays
} an array is a multidimensional table
} the size of an array of dimension k is d1 x d2 x ... x dk
5
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
2
9/19/23
Arrays
} an array is a multidimensional table
} the size of an array of dimension k is d1 x d2 x ... x dk
} in MATLAB
} d1 is the number rows and d2 is the number of columns
6
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
Arrays
} all MATLAB variables are multidimensional arrays
} the size of array in MATLAB:
7
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
3
9/19/23
Arrays
} all MATLAB variables are multidimensional arrays
} the size of array in MATLAB:
>> size([])
8
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
[Fill in here]
Scalars
} a scalar in MATLAB is an array of size 1 x 1
1x1
9
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
4
9/19/23
[Fill in here]
Vectors
} a vector is a 2-dimensional array where one of the size
of one of the dimensions is 1
vector vector
1x3 5x1
10
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
10
>> v = [1 2 3 4]
11
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
Example
11
5
9/19/23
>> v = [1 2 3 4]
v =
1 2 3 4
12
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
Example
12
>> v = [1 2 3 4]
v =
1 2 3 4
>> v = [1, 2, 3, 4]
13
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
Example
13
6
9/19/23
>> v = [1 2 3 4]
v =
1 2 3 4
>> v = [1, 2, 3, 4]
v =
1 2 3 4
14
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
Example
14
>> v = 1:4
15
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
Example
15
7
9/19/23
>> v = 1:4
v =
1 2 3 4
16
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
Example
16
>> v = 1:2:9
17
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
Example
17
8
9/19/23
>> v = 1:2:9
v =
1 3 5 7 9
18
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
Example
18
>> v = 1:2:9
v =
1 3 5 7 9
19
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
Example
19
9
9/19/23
>> v = 1:2:9
v =
1 3 5 7 9
20
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
Example
20
>> start = 5;
21
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
Example
21
10
9/19/23
>> start = 5;
>> step = 5;
22
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
Example
22
>> start = 5;
>> step = 5;
>> stop = 25;
23
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
Example
23
11
9/19/23
>> start = 5;
>> step = 5;
>> stop = 25;
>> v = start:step:stop
24
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
Example
24
>> start = 5;
>> step = 5;
>> stop = 25;
>> v = start:step:stop
v =
5 10 15 20 25
25
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
Example
25
12
9/19/23
26
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
Example
26
27
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
Example
27
13
9/19/23
28
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
Example
28
29
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
Example
29
14
9/19/23
v =
25 20 15 10 5
30
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
Example
30
31
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
Example
31
15
9/19/23
32
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
Example
32
33
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
Example
33
16
9/19/23
[Fill in here]
Creating row vectors
} observe that the stop value is not guaranteed to be at
the end of the vector
34
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
Example
34
[Fill in here]
Creating row vectors
} observe that the stop value is not guaranteed to be at
the end of the vector
v =
25 20 15 10
35
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
Example
35
17
9/19/23
36
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
36
37
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
Example
37
18
9/19/23
[Fill in here]
Creating row vectors
} the function linspace will generate a linearly spaced
vector that includes the start and end values by
calculating the step size for you
>> help linspace
linspace Linearly spaced vector.
linspace(X1, X2) generates a row vector of 100 linearly
equally spaced points between X1 and X2.
Ans =
14
38
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
Example
38
>> v = [1; 2; 3; 4]
39
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
Example
39
19
9/19/23
>> v = [1; 2; 3; 4]
v =
1
2
3
4
40
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
Example
40
>> v = [start:step:stop]'
41
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
Example
41
20
9/19/23
>> v = [start:step:stop]'
v =
25
20
15
10
42
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
Example
42
43
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
Example
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
43
21
9/19/23
44
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
Example
44
45
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
Example
45
22
9/19/23
>> v = 1:4;
46
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
Example
46
[Fill in here]
Creating column vectors
} a column vector can be created from a row vector by
using the colon notation like so
>> v = 1:4;
>> w = v( )
47
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
Example
47
23
9/19/23
[Fill in here]
Creating column vectors
} a column vector can be created from a row vector by
using the colon notation like so
notice that the colon has
>> v = 1:4; two different uses in this
>> w = v( ) example
w =
1
2
3
4
48
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
Example
48
>> v = [1 2 3 4];
49
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
Example
49
24
9/19/23
>> v = [1 2 3 4];
>> length(v)
50
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
Example
50
[Fill in here]
Number of elements in a vector
} the function length will return the number of
elements in the vector
>> v = [1 2 3 4];
>> length(v)
ans =
51
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
Example
51
25
9/19/23
[Fill in here]
Number of elements in a vector
} the function length will return the number of
elements in the vector
the function length does
>> v = [1 2 3 4]; not compute the Euclidean
>> (v) length of a vector!
ans =
52
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
Example
52
Magnitude of a vector
53
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
53
26
9/19/23
Magnitude of a vector
54
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
54
Magnitude of a vector
55
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
55
27
9/19/23
Magnitude of a vector
} use the norm function to compute the vector norm
} by default norm computes the Euclidean norm
56
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
56
Magnitude of a vector
} use the norm function to compute the vector norm
} by default norm computes the Euclidean norm
>> v = [1 1];
57
Example
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
57
28
9/19/23
[Fill in here]
Magnitude of a vector
} use the norm function to compute the vector norm
} by default norm computes the Euclidean norm
>> v = [1 1];
>>
58
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
Example
58
[Fill in here]
Magnitude of a vector
} use the norm function to compute the vector norm
} by default norm computes the Euclidean norm
>> v = [1 1];
>>
ans =
1.4142
59
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
Example
59
29
9/19/23
60
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
60
61
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
61
30
9/19/23
62
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
62
63
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
Example
63
31
9/19/23
v =
-5 -4 -3 -2 -1 0 1 2 3
64
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
Example
64
v =
-5 -4 -3 -2 -1 0 1 2 3
ans =
-5
65
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
Example
65
32
9/19/23
v =
-5 -4 -3 -2 -1 0 1 2 3
ans =
-4
66
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
Example
66
[Fill in here]
Indexing elements of a vector
>> v = -5:3
v =
-5 -4 -3 -2 -1 0 1 2 3
v =
-5 -4 -2 -1 0 1 2 3
67
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
Example
67
33
9/19/23
68
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
Example
68
v =
-5 -4 -3 -2 -1 0 1 2 3
69
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
Example
69
34
9/19/23
[Fill in here]
Indexing elements of a vector
} the keyword end can be used to access the last
element of the vector
>> v = -5:3
v =
-5 -4 -3 -2 -1 0 1 2 3
ans =
70
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
Example
70
[Fill in here]
Indexing elements of a vector
} you can use arithmetic with end
>> v = -5:3
v =
-5 -4 -3 -2 -1 0 1 2 3
ans =
71
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
Example
71
35
9/19/23
>> v = -5:3
v =
-5 -4 -3 -2 -1 0 1 2 3
>> v([1 3 5]) get a vector of the first, third and fifth elements of v
ans =
-5 -3 -1
72
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
Example
72
>> v = -5:3
v =
-5 -4 -3 -2 -1 0 1 2 3
>> v([1 3 5]) = [7 8 9] set the first, third and fifth elements of v
v =
7 -4 8 -2 9 0 1 2 3
73
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
Example
73
36
9/19/23
[Fill in here]
Reminder: Scalars vs. Arrays.
74
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
74
75
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
75
37
9/19/23
[Fill in here]
76
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
Example
76
[Fill in here]
w =
1 0 -1
77
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
Example
77
38
9/19/23
[Fill in here]
78
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
Example
78
[Fill in here]
w =
0.5000 1.0000 1.5000
79
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
Example
79
39
9/19/23
[Fill in here]
w =
1 4 9
80
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
Example
80
[Fill in here]
Arithmetic operations with arrays
} you can perform element-by-element arithmetic with
two arrays of the same size
operator name
+ addition
- subtraction
.* multiplication
right array division
.\ left array division
.^ array power
81
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
81
40
9/19/23
[Fill in here]
w =
8 10 12
82
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
Example
82
[Fill in here]
w =
-6 -6 -6
83
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
Example
83
41
9/19/23
[Fill in here]
84
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
Example
84
[Fill in here]
85
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
Example
85
42
9/19/23
[Fill in here]
86
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
Example
86
[Fill in here]
87
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
Example
87
43
9/19/23
Examples
} compute the unit vector u of a non-unit vector v
>> u = v / norm(v);
88
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
Example
88
Examples
} compute the unit vector u of a non-unit vector v
>> u = v / norm(v);
>> m = (p + q) / 2; % or
>> m = 0.5 * (p + q);
89
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
Example
89
44
9/19/23
Examples
} compute the dot product of vectors u and v
90
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
Example
90
Examples
} compute the dot product of vectors u and v
91
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
Example
91
45
9/19/23
Examples
} plot the standard normal distribution on the interval
[-5, 5]
92
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
Example
92
Examples
} plot the standard normal distribution on the interval
[-5, 5]
93
Copyright Jam es Andrew Sm ith & Others 2015 – 2023. No perm ission granted to reuse. Do no share or distribute outside of York University
Example
93
46