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
A Motivating Experiment:
Creating Vectors
Listing Elements >> my_vector = [ ]; or >> my_vector = [1, 4, 30, 2, 6];
>> my_vector = [1:10]; or >> my_vector = [1:0.1:10]; or >> my_vector = 1:0.1:10; Using the Colon Operator :
Using a Built-in Function >> x = 0:0.1:2*pi; >> my_vector = sin(x); Or >> x = [0:10]; >> my_vector = x*5;
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)
Column Vectors >> a = [1; 2; 3; 4; 5] >> a = [ ]
Transposing a Row Vector >> a = [ ] a = >> b = a' b =
Demo 1: Accessing Data in Vectors
Accessing One or More Values Accessing Individual Values >> value = my_vector(5); Accessing Multiple Values >> values = my_vector([ ]);
Demo 2: Manipulating Vectors
Manipulating Vectors: Basic Maths
Scalar-Array Maths Example >> x = [0:10]; y = x + 5; Or >> x = [0:10]; y = x * 5;
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
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 = Power >> a = [2 3]; >> b = [4 5]; >> c = a.^ b c =
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