0% found this document useful (0 votes)
7 views46 pages

Day05 and 06 Vectors and Matrices 2023 StudentVersion v4

The document is a class session outline for EECS 1011, focusing on vectors and matrices, specifically in the context of MATLAB. It covers the definition and creation of arrays, scalars, and vectors, including practical examples of how to create row vectors using different methods. The content is intended for students at York University and emphasizes hands-on learning with MATLAB programming.

Uploaded by

tatopla20
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views46 pages

Day05 and 06 Vectors and Matrices 2023 StudentVersion v4

The document is a class session outline for EECS 1011, focusing on vectors and matrices, specifically in the context of MATLAB. It covers the definition and creation of arrays, scalars, and vectors, including practical examples of how to create row vectors using different methods. The content is intended for students at York University and emphasizes hands-on learning with MATLAB programming.

Uploaded by

tatopla20
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 46

9/19/23

Vectors and Matrices

EECS 1011 Class Session 5

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.

Section 1.6. Vectors


and Matrices

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

1x1 1x3 5x1 4x2 2x3x5

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:

>> help size

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:

>> help size

} the notion of an empty array exists

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

Creating row vectors


} a row vector can be created directly by entering the
values of the vector inside a pair of square brackets
with the values separated by spaces or commas

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

Creating row vectors


} a row vector can be created directly by entering the
values of the vector inside a pair of square brackets
with the values separated by spaces or commas

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

Creating row vectors


} a row vector can be created directly by entering the
values of the vector inside a pair of square brackets
with the values separated by spaces or commas

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

Creating row vectors


} a row vector can be created directly by entering the
values of the vector inside a pair of square brackets
with the values separated by spaces or commas

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

Creating row vectors


} the colon operator can be used to create row vectors
having values that are equally spaced

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

Creating row vectors


} the colon operator can be used to create row vectors
having values that are equally spaced

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

Creating row vectors


} you can specify the spacing of values using the colon
operator

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

Creating row vectors


} you can specify the spacing of values using the colon
operator

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

Creating row vectors


} you can specify the spacing of values using the colon
operator

>> v = 1:2:9

v =

1 3 5 7 9

>> v = 1:2:8 what does this result in?

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

Creating row vectors


} you can specify the spacing of values using the colon
operator

>> v = 1:2:9

v =

1 3 5 7 9

>> v = 1:2:8 what does this result in?

>> v = 8:1 and this?

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

Creating row vectors


} you can specify the spacing of values using the colon
operator

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

Creating row vectors


} you can specify the spacing of values using the colon
operator

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

Creating row vectors


} you can specify the spacing of values using the colon
operator

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

Creating row vectors


} you can specify the spacing of values using the colon
operator

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

Creating row vectors


} you can specify the spacing of values using the colon
operator

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

Creating row vectors


} the step size can be negative if start > stop

>> start = 25;

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

Creating row vectors


} the step size can be negative if start > stop

>> start = 25;


>> step = -5;

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

Creating row vectors


} the step size can be negative if start > stop

>> start = 25;


>> step = -5;
>> stop = 5;

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

Creating row vectors


} the step size can be negative if start > stop

>> start = 25;


>> step = -5;
>> stop = 5;
>> v = start:step:stop

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

Creating row vectors


} the step size can be negative if start > stop

>> start = 25;


>> step = -5;
>> stop = 5;
>> v = start:step:stop

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

Creating row vectors


} observe that the stop value is not guaranteed to be at
the end of the vector

>> start = 25;

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

Creating row vectors


} observe that the stop value is not guaranteed to be at
the end of the vector

>> start = 25;


>> step = -5;

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

Creating row vectors


} observe that the stop value is not guaranteed to be at
the end of the vector

>> start = 25;


>> step = -5;
>> stop = 6;

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

>> start = 25;


>> step = -5;
>> stop = 6;
>> v = :step:

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

>> start = 25;


>> step = -5;
>> stop = 6;
>> v = :step:

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

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

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

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.

linspace(X1, X2, N) generates N points between X1 and X2.


For N = 1, linspace returns X2 .

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.

linspace(X1, X2, N) generates N points between X1 and X2.


For N = 1, linspace returns X2 .
>> linspace( )

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

Creating column vectors


} a column vector can be created directly by entering the
values of the vector inside a pair of square brackets
with the values separated by semi-colons

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

Creating column vectors


} a column vector can be created directly by entering the
values of the vector inside a pair of square brackets
with the values separated by semi-colons

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

Creating column vectors


} a column vector can be created from a row vector by
transposing the row vector

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

Creating column vectors


} a column vector can be created from a row vector by
transposing the row vector

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

Creating column vectors


} a column vector can be created from a row vector by
transposing the row vector
the single quote after
>> v = [start:step:stop]' after a vector or matrix
will compute the transpose*
of the vector or matrix
v =
25
20
15
10

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

Creating column vectors


} a column vector can be created from a row vector by
transposing the row vector
the single quote after
>> v = [start:step:stop]' after a vector or matrix
will compute the transpose*
of the vector or matrix
v =
*strictly speaking, the single
25 quote is conjugate transpose
20 operator
15
10

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

Creating column vectors


} a column vector can be created from a row vector by
transposing the row vector
the single quote after
>> v = [start:step:stop]' after a vector or matrix
will compute the transpose*
of the vector or matrix
v =
*strictly speaking, the single
25 quote is conjugate transpose
20 operator
15 .' is the transpose operator
10

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

Creating column vectors


} a column vector can be created from a row vector by
using the colon notation like so

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

Number of elements in a vector


} the function length will return the number of
elements in the vector

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

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)

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

} the magnitude of a vector is what mathematicians call


the norm of the 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

} the magnitude of a vector is what mathematicians call


the norm of the vector
} there are many different norms

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

} the magnitude of a vector is what mathematicians call


the norm of the vector
} there are many different norms
} Euclidean norm (Euclidean length, L2 norm, L2 distance)
} taxicab norm (Manhattan norm, Manhattan distance, L1
norm)
} and more...

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

Indexing elements of a vector


} the elements of the vector can be accessed by using an
integer value called an index

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

Indexing elements of a vector


} the elements of the vector can be accessed by using an
integer value called an index
} MATLAB uses a 1-based index
} the first element of the vector has index 1
} the second element has index 2, etc.

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

Indexing elements of a vector


} the elements of the vector can be accessed by using an
integer value called an index
} MATLAB uses a 1-based index
} the first element of the vector has index 1
} the second element has index 2, etc.

} use an index inside of ( ) after the vector name to


access an element of the vector

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

Indexing elements of a vector


>> v = -5:3

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

Indexing elements of a vector


>> v = -5:3

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

Indexing elements of a vector


>> v = -5:3

v =

-5 -4 -3 -2 -1 0 1 2 3

>> v(1) get the value of the first element in v

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

Indexing elements of a vector


>> v = -5:3

v =

-5 -4 -3 -2 -1 0 1 2 3

>> v(2) get the value of the second element in v

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

>> set the value of the third element in v to 100

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

Indexing elements of a vector


} the keyword end can be used to access the last
element of the vector
>> v = -5:3

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

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

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

>> get the value of the last element in v

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

>> get the value of the second last element in v

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

Indexing elements of a vector


} the index does not need to be a scalar
} it can also be a vector of indices!

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

Indexing elements of a vector


} the index does not need to be a scalar
} it can also be a vector of indices!

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

Arithmetic operations with arrays


} you can perform element-by-element arithmetic with
an array and a scalar
operator name
+ addition
- subtraction
* multiplication
/ right division
\ left division
.^ array power

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]

>> u = [1 2 3]; array scalar addition


>>
w =
3 4 5

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]

>> u = [1 2 3]; array scalar subtraction

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]

>> u = [1 2 3]; array scalar


multiplication
w =
2 4 6

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]

>> u = [1 2 3]; array scalar division

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]

>> u = [1 2 3]; array power

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]

>> u = [1 2 3]; array addition


>> v = [7 8 9];

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]

>> u = [1 2 3]; array subtraction


>> v = [7 8 9];

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]

>> u = [1 2 3]; array multiplication


>> v = [7 8 9];
in mathematics,
called the Hadamard
w = product or the Schur
7 16 27 product

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]

>> u = [1 2 3]; right array division


>> v = [7 8 9];
the elements in u
divided by the
w =
elements in v
0.1429 0.2500 0.3333

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]

>> u = [1 2 3]; left array division


>> v = [7 8 9];
>> w = u .\ v the elements in v
divided by the
w =
elements in u
7 4 3

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]

>> u = [1 2 3]; array power


>> v = [4 5 6];
the elements in u
raised to the
w =
corresponding
1 32 729 power in v

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);

} compute the midpoint m of two points p and q

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

>> dprod = sum(u .* v); % or


>> dprod = dot(u, 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

>> dprod = sum(u .* v); % or


>> dprod = dot(u, v);

} compute the body mass index where m is a vector of


masses (in kg) and h is a vector of heights (in m)

>> bmi = m ./ (h .* h); % or


>> bmi = m ./ h .^ 2;

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]

>> x = linspace(-5, 5);


>> y = exp(-0.5 * x .^ 2) / sqrt(2 * pi);
>> plot(x, y);

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

You might also like