Вы находитесь на странице: 1из 6

GENN004: Introduction to Computers

09-Feb-13

Vectorized Code

Outline
Operations on Vectors and Matrices Vectors and Matrices as Function Arguments Logical Vectors

Vectorized Code

GENN004: Introduction to Computers

09-Feb-13

Operations on Vectors and Matrices


v=[3 7 2 1] for i = 1:length(v) v(i) =v(i) * 3; End v= [3 7 2 1]; >> v=v*3 9 27 6 3 mat = [4:6; 3:1:1] mat = 4 5 6 3 2 1 >> mat * 2 ans = 8 10 12 6 4 2

Scalar Operations
Operation Addition Subtraction Multiplication Division Exponentiation Algebraic Form a+b ab ab a b ab MATLAB Form a + b a b a * b a / b or b \ a a ^ b

Note that b \ a is called left division This is equal to a / b

Vectorized Code

GENN004: Introduction to Computers

09-Feb-13

Array Operations
Operation
Addition Subtraction Multiplication

MATLAB form
a + b a b a .* b

Comments
Adds each element in a to the element with the same index in b Subtracts from each element in a the element with the same index in b Multiplies each element in a with the element in b having the same index Either a or b can be a scalar Element by element division of a and b: a(i,j) / b(i,j) provided a, b have same shape Either a or b can be a scalar Element by element division of a and b: b(i,j) / a(i,j) provided a, b have same shape Either a or b can be a scalar Element by element exponentiation of a and b: a(i,j) ^ b(i,j) provided a, b have same shape Either a or b can be a scalar

Right Division

a ./ b

Left Division

a .\ b

Exponentiation

a .^ b

Array and Matrix Operations


Array Operations are performed element-by-element
Both arrays must have the same number of rows and columns

Matrix Operations follow rules of linear algebra


These rules are specific to each operation Useful after Linear Algebra Course

Be careful not to confuse these two operation sets


Since a matrix is an array in MATLAB, many times picking the wrong operation wont produce an error as it is technically valid but logically incorrect

Vectorized Code

GENN004: Introduction to Computers

09-Feb-13

Operations Rules Examples


v=[3 7 2 1]; >> v ^ 2 ??? Error using ==> mpower Inputs must be a scalar and a square matrix. To compute elementwise POWER, use POWER (.^) instead. >> v .^ 2 ans = 9 49 4 1

Operations Rules Examples


v1 = 2:5; v2 = [33 11 5 1]; >> v1 * v2 %Error why >> v1*v2 % what is the output >> v1 .* v2 % v1 and v2 must have same size

Vectorized Code

GENN004: Introduction to Computers

09-Feb-13

Vectors and Matrices as Function Arguments


Many functions accept scalars as input Some functions work on arrays Most scalar functions accept arrays as well
The function is performed on each element in the array individually

Try x = pi/2; y = sin(x) in MATLAB Now try x = [0 pi/2 pi 3*pi/2 2*pi]; y = sin(x)

Functions Examples
v1=[1 3 2 7 4 -2] v2=[5 3 4 1 2 -2] [mx mxi]=max(v1) v=v1-v2 v=sign(v1-v2) x=sum(v1) v=find(v1>3)

7 4 -4 0 -2 6 2 0 -1 0 -1 1 1 0 15 4 5

Vectorized Code

GENN004: Introduction to Computers

09-Feb-13

Logical Vectors
v1=[1 3 2 7 4 -2] v2=[5 3 -4 -1 2 -2] v=v1>0 v=v2>0 a=vec(v) x=sum(v2>0) v=true(1,5) v=false(1,5) x=all(v) y=any(v) 1 1 1 1 1 0 1 1 0 0 1 0 5 3 2 3 1 1 1 1 1 0 0 0 0 0 1 (are all ones?) 0 (any one?)

Example: Vertical Motion


% Vertical motion under gravity g = 9.81; % acceleration due to gravity u = 60; % initial velocity in metres/sec t = 0 : 0.1 : 12.3; % time in seconds s = u * t g / 2 * t . 2; % vertical displacement in metres plot(t, s), title( Vertical motion under gravity ) xlabel( time ), ylabel( vertical displacement ) grid disp( [t s] ) % display a table

Vectorized Code