vectors lecture 11: supporting material dr kathryn merrick tuesday 7 th april, 2009

19
Vectors Lecture 11: Supporting Material Dr Kathryn Merrick Tuesday 7 th April, 2009

Upload: wilfrid-scot-bennett

Post on 17-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Vectors Lecture 11: Supporting Material Dr Kathryn Merrick Tuesday 7 th April, 2009

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
Page 3: Vectors Lecture 11: Supporting Material Dr Kathryn Merrick Tuesday 7 th April, 2009

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

A Motivating Experiment:

Page 5: Vectors Lecture 11: Supporting Material Dr Kathryn Merrick Tuesday 7 th April, 2009

Creating Vectors

Page 6: Vectors Lecture 11: Supporting Material Dr Kathryn Merrick Tuesday 7 th April, 2009

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

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

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

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

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

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

Demo 1:

Accessing Data in Vectors

Page 13: Vectors Lecture 11: Supporting Material Dr Kathryn Merrick Tuesday 7 th April, 2009

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

Demo 2:

Manipulating Vectors

Page 15: Vectors Lecture 11: Supporting Material Dr Kathryn Merrick Tuesday 7 th April, 2009

Manipulating Vectors: Basic Maths

Page 16: Vectors Lecture 11: Supporting Material Dr Kathryn Merrick Tuesday 7 th April, 2009

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

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

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

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