CSCI 101
Vectorized Code
Outline
• Operations on Vectors and Matrices
• Vectors and Matrices as Function Arguments
• Logical Vectors
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 Algebraic Form MATLAB Form
Addition a + b a + b
Subtraction a – b a – b
Multiplication a × b a * b
Division
a
b
a / b
Exponentiation ab a ^ b
Same operators can be applied to arrays following linear algebra rules.
These rules are specific to each operation
A+B (A and B should have same size)
A*B (A columns should equal B rows)
A^B (A should be squared matrix)
A/B = A* B-1 (B should be invertible and A columns should be equal B-1 rows )
Element Operations
Both arrays must have the same number of rows and columns
A + B = [1 2 3] + [3 4 5] = [4 6 8]
Element and Algebraic operations are the same
A .* B = [1 2 3] .* [3 4 5] = [3 8 15]
A .^ B = [4 9 6] .^ [2 2 2] = [16 81 36]
A ./ B =[4 9 6] ./ [2 3 2] = [2 3 3]
Array Operations
Operation MATLAB form Comments
Addition a + b Adds each element in a to the element with the same
index in b
Subtraction a – b Subtracts from each element in a the element with the
same index in b
Multiplication a .* b Multiplies each element in a with the element in b
having the same index
Either a or b can be a scalar
Right Division a ./ b 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
Left Division a . b 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
Exponentiation a .^ b 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
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;
 v1 = [2 3 4 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
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)  7 4
v=v1-v2 -4 0 -2 6 2 0
v=sign(v1-v2) -1 0 -1 1 1 0
x=sum(v1)  15
v=find(v1>3)  4 5
Logical Vectors
v1=[1 3 2 7 4 -2]
v2=[5 3 -4 -1 2 -2]
v=v1>0  1 1 1 1 1 0
v=v2>0 1 1 0 0 1 0
a=v2(v2>0) 5 3 2
x=sum(v2>0) 3
v=true(1,5)  1 1 1 1 1
v=false(1,5)  0 0 0 0 0
x=all(v1>0)  1 (are all ones?)
y=any(v1>0)  0 (any one?)
Example
?)0:),1((ofvaluetheisWhat
5310
2412
3142
0311

















 aBaLet
B = [0 0 0 1]
Example
?)2:1],34([ofvaluetheisWhat
5310
2412
3142
0311
aDaLet 





















 

12
10
D
Example
Write a program using Matlab loop(s) to calculate the following
while assuming that the variable x has been initialized:
𝑆 =
𝑖=1
10
𝑥𝑖
2
N=10;
x = 1:N;
S=0;
for i = 1:N
S=S+ x(i)*x(i);
end
S
Vectorize your code S=sum(x.*x)
Getting Maximum and Minimum Value
v=[1 3 2 7 4 2]
maxv=0;
for =1:length(v)
if v(i)>maxv
maxv=v(i);
maxi=i;
end
end
% Repeat for the minimum
i v(i) maxv maxi
1 1 01 ?1
2 3 13 12
3 2 3 2
4 7 37 24
5 4 7 4
6 2 7 4
Vectorize your code: [maxv maxi]=max(v)
Example
Counting Elements
v=[1 3 2 -7 4 -2]
c=0;
for =1:length(v)
if v(i)>0
c=c+1;
end
end
% Repeat for the negative
i v(i) c
1 1 01
2 3 12
3 2 23
4 -7 3
5 4 34
6 -2 4
Vectorize your code c = sum(v>0)
Example
Comparing Elements
v1=[1 3 2 7 4 -2]
v2=[5 3 4 1 2 -2]
for i=1:length(v1)
if v1(i)>v2(i)
v(i)=1;
else
v(i)=0;
end
end
i v1(i) v2(i) v(i)
1 1 5 0
2 3 3 0
3 2 4 0
4 7 1 1
5 4 2 1
6 -2 -2 0
Vectorize your code: v= v1>v2
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
Example

Csci101 lect09 vectorized_code

  • 1.
  • 2.
    Outline • Operations onVectors and Matrices • Vectors and Matrices as Function Arguments • Logical Vectors
  • 3.
    Operations on Vectorsand 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
  • 4.
    Scalar Operations Operation AlgebraicForm MATLAB Form Addition a + b a + b Subtraction a – b a – b Multiplication a × b a * b Division a b a / b Exponentiation ab a ^ b Same operators can be applied to arrays following linear algebra rules. These rules are specific to each operation A+B (A and B should have same size) A*B (A columns should equal B rows) A^B (A should be squared matrix) A/B = A* B-1 (B should be invertible and A columns should be equal B-1 rows )
  • 5.
    Element Operations Both arraysmust have the same number of rows and columns A + B = [1 2 3] + [3 4 5] = [4 6 8] Element and Algebraic operations are the same A .* B = [1 2 3] .* [3 4 5] = [3 8 15] A .^ B = [4 9 6] .^ [2 2 2] = [16 81 36] A ./ B =[4 9 6] ./ [2 3 2] = [2 3 3]
  • 6.
    Array Operations Operation MATLABform Comments Addition a + b Adds each element in a to the element with the same index in b Subtraction a – b Subtracts from each element in a the element with the same index in b Multiplication a .* b Multiplies each element in a with the element in b having the same index Either a or b can be a scalar Right Division a ./ b 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 Left Division a . b 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 Exponentiation a .^ b 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
  • 7.
    Operations Rules Examples v=[37 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
  • 8.
    Operations Rules Examples v1= 2:5;  v1 = [2 3 4 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
  • 9.
    Vectors and Matricesas 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)
  • 10.
    Functions Examples v1=[1 32 7 4 -2] v2=[5 3 4 1 2 -2] [mx mxi]=max(v1)  7 4 v=v1-v2 -4 0 -2 6 2 0 v=sign(v1-v2) -1 0 -1 1 1 0 x=sum(v1)  15 v=find(v1>3)  4 5
  • 11.
    Logical Vectors v1=[1 32 7 4 -2] v2=[5 3 -4 -1 2 -2] v=v1>0  1 1 1 1 1 0 v=v2>0 1 1 0 0 1 0 a=v2(v2>0) 5 3 2 x=sum(v2>0) 3 v=true(1,5)  1 1 1 1 1 v=false(1,5)  0 0 0 0 0 x=all(v1>0)  1 (are all ones?) y=any(v1>0)  0 (any one?)
  • 12.
  • 13.
  • 14.
    Example Write a programusing Matlab loop(s) to calculate the following while assuming that the variable x has been initialized: 𝑆 = 𝑖=1 10 𝑥𝑖 2 N=10; x = 1:N; S=0; for i = 1:N S=S+ x(i)*x(i); end S Vectorize your code S=sum(x.*x)
  • 15.
    Getting Maximum andMinimum Value v=[1 3 2 7 4 2] maxv=0; for =1:length(v) if v(i)>maxv maxv=v(i); maxi=i; end end % Repeat for the minimum i v(i) maxv maxi 1 1 01 ?1 2 3 13 12 3 2 3 2 4 7 37 24 5 4 7 4 6 2 7 4 Vectorize your code: [maxv maxi]=max(v) Example
  • 16.
    Counting Elements v=[1 32 -7 4 -2] c=0; for =1:length(v) if v(i)>0 c=c+1; end end % Repeat for the negative i v(i) c 1 1 01 2 3 12 3 2 23 4 -7 3 5 4 34 6 -2 4 Vectorize your code c = sum(v>0) Example
  • 17.
    Comparing Elements v1=[1 32 7 4 -2] v2=[5 3 4 1 2 -2] for i=1:length(v1) if v1(i)>v2(i) v(i)=1; else v(i)=0; end end i v1(i) v2(i) v(i) 1 1 5 0 2 3 3 0 3 2 4 0 4 7 1 1 5 4 2 1 6 -2 -2 0 Vectorize your code: v= v1>v2 Example
  • 18.
    Vertical Motion % Verticalmotion 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 Example