Date post: | 17-Jan-2016 |
Category: |
Documents |
Upload: | wilfrid-scot-bennett |
View: | 213 times |
Download: | 0 times |
Vectors
Lecture 11: Supporting Material
Dr Kathryn Merrick
Tuesday 7th 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 = [1 4 30 2 6];
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 = [1
2
3
4
5]
Transposing a Row Vector
>> a = [1 2 3 4 5]
a =
1 2 3 4 5
>> b = a'
b =
1
2
3
4
5
Demo 1:
Accessing Data in Vectors
Accessing One or More Values
Accessing Individual Values
>> value = my_vector(5);
Accessing Multiple Values
>> values = my_vector([5 10 15]);
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 = 0.5 0.6
Power
>> a = [2 3];>> b = [4 5];>> c = a .^ b
c = 16 243
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