Vectors Lecture 11: Supporting Material Dr Kathryn Merrick Tuesday 7 th April, 2009.
-
Upload
wilfrid-scot-bennett -
Category
Documents
-
view
213 -
download
0
Transcript of Vectors Lecture 11: Supporting Material Dr Kathryn Merrick Tuesday 7 th April, 2009.
![Page 1: Vectors Lecture 11: Supporting Material Dr Kathryn Merrick Tuesday 7 th April, 2009.](https://reader036.fdocuments.in/reader036/viewer/2022062804/56649f2a5503460f94c443af/html5/thumbnails/1.jpg)
Vectors
Lecture 11: Supporting Material
Dr Kathryn Merrick
Tuesday 7th April, 2009
![Page 2: Vectors Lecture 11: Supporting Material Dr Kathryn Merrick Tuesday 7 th April, 2009.](https://reader036.fdocuments.in/reader036/viewer/2022062804/56649f2a5503460f94c443af/html5/thumbnails/2.jpg)
![Page 3: Vectors Lecture 11: Supporting Material Dr Kathryn Merrick Tuesday 7 th April, 2009.](https://reader036.fdocuments.in/reader036/viewer/2022062804/56649f2a5503460f94c443af/html5/thumbnails/3.jpg)
Overview
What is a vector?
Creating vectors
Accessing vectors
Manipulating data in vectors
Implicit vectorisation
Reference: text book Ch 5
![Page 4: Vectors Lecture 11: Supporting Material Dr Kathryn Merrick Tuesday 7 th April, 2009.](https://reader036.fdocuments.in/reader036/viewer/2022062804/56649f2a5503460f94c443af/html5/thumbnails/4.jpg)
A Motivating Experiment:
![Page 5: Vectors Lecture 11: Supporting Material Dr Kathryn Merrick Tuesday 7 th April, 2009.](https://reader036.fdocuments.in/reader036/viewer/2022062804/56649f2a5503460f94c443af/html5/thumbnails/5.jpg)
Creating Vectors
![Page 6: Vectors Lecture 11: Supporting Material Dr Kathryn Merrick Tuesday 7 th April, 2009.](https://reader036.fdocuments.in/reader036/viewer/2022062804/56649f2a5503460f94c443af/html5/thumbnails/6.jpg)
Listing Elements
>> my_vector = [1 4 30 2 6];
or
>> my_vector = [1, 4, 30, 2, 6];
![Page 7: Vectors Lecture 11: Supporting Material Dr Kathryn Merrick Tuesday 7 th April, 2009.](https://reader036.fdocuments.in/reader036/viewer/2022062804/56649f2a5503460f94c443af/html5/thumbnails/7.jpg)
>> my_vector = [1:10];
or
>> my_vector = [1:0.1:10];
or
>> my_vector = 1:0.1:10;
Using the Colon Operator :
![Page 8: Vectors Lecture 11: Supporting Material Dr Kathryn Merrick Tuesday 7 th April, 2009.](https://reader036.fdocuments.in/reader036/viewer/2022062804/56649f2a5503460f94c443af/html5/thumbnails/8.jpg)
Using a Built-in Function
>> x = 0:0.1:2*pi;
>> my_vector = sin(x);
Or
>> x = [0:10];
>> my_vector = x*5;
![Page 9: Vectors Lecture 11: Supporting Material Dr Kathryn Merrick Tuesday 7 th April, 2009.](https://reader036.fdocuments.in/reader036/viewer/2022062804/56649f2a5503460f94c443af/html5/thumbnails/9.jpg)
Using linspace or logspace
linspace(x, y) generates 100 equally spaced points between x and y
>> my_vector = linspace(1, 100)
linspace(x, y, n) generates n equally spaced points between x and y
>> my_vector = linspace(1, 100, 10)
![Page 10: Vectors Lecture 11: Supporting Material Dr Kathryn Merrick Tuesday 7 th April, 2009.](https://reader036.fdocuments.in/reader036/viewer/2022062804/56649f2a5503460f94c443af/html5/thumbnails/10.jpg)
Column Vectors
>> a = [1; 2; 3; 4; 5]
>> a = [1
2
3
4
5]
![Page 11: Vectors Lecture 11: Supporting Material Dr Kathryn Merrick Tuesday 7 th April, 2009.](https://reader036.fdocuments.in/reader036/viewer/2022062804/56649f2a5503460f94c443af/html5/thumbnails/11.jpg)
Transposing a Row Vector
>> a = [1 2 3 4 5]
a =
1 2 3 4 5
>> b = a'
b =
1
2
3
4
5
![Page 12: Vectors Lecture 11: Supporting Material Dr Kathryn Merrick Tuesday 7 th April, 2009.](https://reader036.fdocuments.in/reader036/viewer/2022062804/56649f2a5503460f94c443af/html5/thumbnails/12.jpg)
Demo 1:
Accessing Data in Vectors
![Page 13: Vectors Lecture 11: Supporting Material Dr Kathryn Merrick Tuesday 7 th April, 2009.](https://reader036.fdocuments.in/reader036/viewer/2022062804/56649f2a5503460f94c443af/html5/thumbnails/13.jpg)
Accessing One or More Values
Accessing Individual Values
>> value = my_vector(5);
Accessing Multiple Values
>> values = my_vector([5 10 15]);
![Page 14: Vectors Lecture 11: Supporting Material Dr Kathryn Merrick Tuesday 7 th April, 2009.](https://reader036.fdocuments.in/reader036/viewer/2022062804/56649f2a5503460f94c443af/html5/thumbnails/14.jpg)
Demo 2:
Manipulating Vectors
![Page 15: Vectors Lecture 11: Supporting Material Dr Kathryn Merrick Tuesday 7 th April, 2009.](https://reader036.fdocuments.in/reader036/viewer/2022062804/56649f2a5503460f94c443af/html5/thumbnails/15.jpg)
Manipulating Vectors: Basic Maths
![Page 16: Vectors Lecture 11: Supporting Material Dr Kathryn Merrick Tuesday 7 th April, 2009.](https://reader036.fdocuments.in/reader036/viewer/2022062804/56649f2a5503460f94c443af/html5/thumbnails/16.jpg)
Scalar-Array Maths Example
>> x = [0:10];
y = x + 5;
Or
>> x = [0:10];
y = x * 5;
![Page 17: Vectors Lecture 11: Supporting Material Dr Kathryn Merrick Tuesday 7 th April, 2009.](https://reader036.fdocuments.in/reader036/viewer/2022062804/56649f2a5503460f94c443af/html5/thumbnails/17.jpg)
Basic Array Maths
Addition
>> a = [2 3];
>> b = [4 5];
>> c = a + b
c =
6 8
Subtraction
>> a = [2 3];>> b = [4 5];>> c = a - b
c = -2 -2
![Page 18: Vectors Lecture 11: Supporting Material Dr Kathryn Merrick Tuesday 7 th April, 2009.](https://reader036.fdocuments.in/reader036/viewer/2022062804/56649f2a5503460f94c443af/html5/thumbnails/18.jpg)
Basic Array Maths
Multiplication
>> a = [2 3];>> b = [4 5];>> c = a .* b
c = 8 15
Division
>> a = [2 3];>> b = [4 5];>> c = a ./ b
c = 0.5 0.6
Power
>> a = [2 3];>> b = [4 5];>> c = a .^ b
c = 16 243
![Page 19: Vectors Lecture 11: Supporting Material Dr Kathryn Merrick Tuesday 7 th April, 2009.](https://reader036.fdocuments.in/reader036/viewer/2022062804/56649f2a5503460f94c443af/html5/thumbnails/19.jpg)
Summary
After today’s lecture you should be able to:
Create and access data in vectors
Manipulate data in vectors using loops or implicit vectorisation