You are on page 1of 12

Vectors 4.

1

4. Vectors: one-dimensional arrays
MATLAB was first developed for use with matrices (MATRIX LABoratory). All the
usual matrix operations are available. Also, MATLAB can work with arrays without
considering them as vectors. In this form we introduce new types of operation called
array processes, in particular array multiplication and array division. It is
particularly important for developing graphs of functions (see section 2.1).

4.1 Geometric vectors
Vector scalar product
Find the angle, u , between the two vectors

1
2 and 2
cos u
÷
= ÷
| |
=
|
|
\ .
a i j +k b = i - j +k
a.b
a b

>> clear all
>> format short
>> a=[1,-2,1]; b=[2,-1,1];
>> theta_in_degrees=(360/pi)*acos(a*b'/(sqrt(a*a')*sqrt(b*b')))
theta_in_degrees =
67.1146
Here we have defined the vectors a and b as row vectors and used the transpose, ' b ,
to evaluate the scalar product. Alternatively, we could define b as a column vector
>> clear all
>> a=[1,-2,1]; b=[2;-1;1]; % ; makes b a column vector
>> theta_in_degrees=(360/pi)*acos(a*b/(sqrt(a*a')*sqrt(b'*b)))
theta_in_degrees =
67.1146
N.B. Elements in rows are separated by commas or spaces and the vectors are
enclosed in square brackets. The end of a row is defined by a semi-colon ‘;’.

4.2 Row and column vectors
Row vectors
The number of entries is known as the ‘length’ of the vector.
>> a=[-3,2^3,sqrt(7)]
a =
-3.0000 8.0000 2.6458
Vectors 4.2

>> length(a)
ans =
3
Take care, spaces are very important as we now see:
>> a2=[1+ 3 6]
a2 =
4 6
>> a3=[1 +3 6]
a3 =
1 3 6
We can do the usual arithmetic operations with vectors of the same length, such as a
and a3:
>> a+a3
ans =
-2.0000 11.0000 8.6458
>> a4=4*a
a4 =
-12.0000 32.0000 10.5830
>> a5=3*a-4*a3
a5 =
-13.0000 12.0000 -16.0627
>> a+a2
??? Error using ==> +
Matrix dimensions must agree.
i.e. the error is due to the fact that a and a2 have different lengths.
A vector may be multiplied by a scalar or added or subtracted to or from another
vector of the same length. The operations are carried out elementwise.
We can build row vectors from existing ones:
>> clear all % clear all variable names occasionally
>> a=[-1, 3 2], b=[6, 4]
a =
-1 3 2
b =
6 4
>> c=[3*b, -2*a], sort(c)
c =
18 12 2 -6 -4
ans =
-6 -4 2 12 18
Vectors 4.3

Notice that the last command sorted the elements of cd into ascending order.
We can also change or look at the value of particular entries:
>> a(3), a(2)=-7
ans =
2
a =
-1 -7 2
The colon notation
This is a shortcut for producing row vectors:
>> 1:5
ans =
1 2 3 4 5
>> 4:8
ans =
4 5 6 7 8
>> -2:3
ans =
-2 -1 0 1 2 3
>> 2:-4
ans =
Empty matrix: 1-by-0
In general a:b:c produces a vector of entries starting with the value a, incrementing
by the value b until it gets to c (it will not produce a value greater than c). This is why
2:-4 produced the result Empty matrix: 1-by-0.
>> 0.2:0.025:0.3
ans =
0.2000 0.2250 0.2500 0.2750 0.3000
>> -2:1:4
ans =
-2 -1 0 1 2 3 4
>> 0:0.25:2.5
ans =
Columns 1 through 7
0 0.2500 0.5000 0.7500 1.0000
1.2500 1.5000
Columns 8 through 11
1.7500 2.0000 2.2500 2.5000
Vectors 4.4

Extracting bits of a vector
>> u=[1:3:7, -2:-3:-8]
u =
1 4 7 -2 -5 -8
To get the 3
rd
to 6
th
entries:
>> u(3:6)
ans =
7 -2 -5 -8
To get the odd entries:
>> u(1:2:6)
ans =
1 7 -5
Column vectors
These have similar constructs to row vectors. When defining them, entries are
separated by a semicolon “;” or by placing the entries on newlines.
>> clear all
>> format short
>> a=[-1; 3^2; sqrt(7)]
a =
-1.0000
9.0000
2.6458
>> a2=[4 % press return to enter numbers on new line
1
5]
a2 =
4
1
5
>> a3=a/3-2*a2
a3 =
-8.3333
1.0000
-9.1181
so column vectors may be added or subtracted provided that they have the same
length.
Vectors 4.5

Transposition
We can convert a row vector into a column vector, and vice versa, by a process called
transposition denoted in MATLAB by '. In matrix algebra we often write the
transpose as
| |
...
T
.
>> v=[1, 2, 3]; w=[-2; 3; 4];
>> v, v', w, w'
v =
1 2 3
ans =
1
2
3
w =
-2
3
4
ans =
-2 3 4
and we can perform operations on the vectors provided that they are of the same type
and length:
>> s=2*v-w'/3
s =
2.6667 3.0000 4.6667
>> t=4*w+2*v'
t =
-6
16
22
If x is a complex vector, then x’ gives the complex conjugate transpose of x:
>> x=[2+sqrt(-2), 1-3j]
x =
2.0000 + 1.4142i 1.0000 - 3.0000i
>> x'
ans =
2.0000 - 1.4142i
1.0000 + 3.0000i
Vectors 4.6

Note that the components of x were defined without the * operator; this means of
defining complex numbers works even when the variable i already has a numeric
value. To obtain the plain transpose of a complex number use .’ as in
>> x.'
ans =
2.0000 + 1.4142i
1.0000 - 3.0000i

4.3 Multiplication and division of vectors
Scalar product (*)
There are two ways in which we may produce the product of two vectors. In both
cases the vectors concerned must have the same length.
The first product is the standard scalar product. We have already used this to obtain
the scalar product of geometric vectors in section 4.1.
Suppose that a and b are two vectors of length n, a being a row vector and b being a
column vector:
| |
1
2
1 2
, , , ,
n
n
b
b
a a a
b
(
(
(
= =
(
(
(
¸ ¸
a b
The scalar product is defined by multiplying the corresponding elements together and
adding the results to give a single number, a scalar.
1
n
i i
i
a b
=
=
¿
ab
For example, if | | 1, 2, 4 = ÷ a and
5
1
2
÷ (
(
=
(
( ÷
¸ ¸
b
then 3 n = and
( ) ( ) ( ) 1 5 2 1 4 2 15 = × ÷ + ÷ × + × ÷ = ÷ ab
We can perform this product in MATLAB by
>> a=[1, -2, 4], b=[-5; 1; -2]
a =
1 -2 4
b =
-5
Vectors 4.7

1
-2
>> product=a*b
product =
-15
Now, suppose that we also define a row vector c and we wish to form the scalar
products of a with c.
>> c=[2, 0, -3];
>> a*c
??? Error using ==> *
Inner matrix dimensions must agree.
an error results because c is not a column vector.
Recall that transposing with ' turns column vectors into row vectors and vice versa.
So, to form the scalar product of two row vectors or two column vectors we must use
the transpose appropriately.
>> a*c', b'*b
ans =
-10
ans =
30
We shall refer to the Euclidean length of a vector as the norm of a vector; it is denoted
by the symbol a and defined by
2
1
n
i
i
a
=
=
¿
a
where n is its dimension. This can be computed in MATLAB in one of two ways:
>> norm(a), sqrt(a*a')
ans =
4.5826
ans =
4.5826
where norm is a built-in MATLAB function that accepts a vector as input and delivers
a scalar as output. It can also be used to compute other norms:
Array product (.*)
A second way of forming the product of two vectors of the same length is called the
array product or the Hadamard product. It is not often used in Mathematics but is an
essential MATLAB feature. It involves vectors of the same type. If a and b are two
Vectors 4.8

vectors of the same type, i.e. both row vectors or both column vectors, the
mathematical definition of this product is the vector having the components
| |
1 1 2 2
, , ,
n n
a b a b a b = a.b .
The result is a vector of the same length and type as a and b. Thus, we simply
multiply the corresponding elements of two vectors.
In MATLAB, the product is computed with the operator .*.
>> clear all
>> a=[1, -2, 4]; b=[-5; 1; 2];
>> b.*a'
ans =
-5
-2
8
>> a.*b'
ans =
-5 -2 8
We can use the array product if we wish to tabulate a function. Consider sin y x x t =
for 0, 0.25, ,1 x = .
To evaluate y we have to multiply each element of the vector x by the corresponding
element of the vector sin x t :
sin sin
0 0 0
0.2500 0.7071 0.1768
0.5000 1.000 0.5000
0.7500 .7071 0.5303
1.0000 0.0000 0.0000
x x x x t t × =
× =
× =
× =
× =
× =

In MATLAB we proceed as follows:
>> x=[0:0.25:1]';
>> y=x.*sin(pi*x)
y =
0
0.1768
0.5000
0.5303
0.0000
N.B. (i) the use of pi; it is a scalar, i.e. a number, and so does not need an array
product.
Vectors 4.9

(ii) x and sin(pi*x) are both column vectors, the sin function is applied to
each element of the vector, so that the array product of these vectors is also a column
vector.

Array division (./)
There is no mathematical definition for the division of one vector by another.
However, in MATLAB, the operator ./ is defined to give element by element
division; it is therefore only defined for vectors of the same size and type.
>> clear all
>> a=[3:7], b=[1:0.2:1.8], a./b, a./a
a =
3 4 5 6 7
b =
1.0000 1.2000 1.4000 1.6000 1.8000
ans =
3.0000 3.3333 3.5714 3.7500 3.8889
ans =
1 1 1 1 1
We know that division by zero causes problems. Let’s now see how MATLAB deals
with this situation.
>> c=[2, 0, 1, -1, 2], a-6
c =
2 0 1 -1 2
ans =
-3 -2 -1 0 1
>> ans./c
Warning: Divide by zero.
ans =
-1.5000 -Inf -1.0000 0 0.5000
This calculation requires division by 0; and MATLAB returns Inf, denoting infinity,
in the answer. We notice that to evaluate a-6 MATLAB treats the expression as
| | 6 6 6 6 6 a ÷ .
Vectors 4.10

>> a-4
ans =
-1 0 1 2 3
>> ans./c
Warning: Divide by zero.
ans =
1 NaN 1 1 1
Here we are warned about 0/0, giving a NaN, MATLAB for Not a Number.
We shall use array division to estimate the limit
0
sin
lim
x
x
x
÷

The idea is to observe the behaviour of the ratio sin x x for a sequence of values of x
that approach zero. Suppose that we choose the sequence defined by the column
vector | | 0.1 0.01 0.001 0.0001 0.00001 0.000001 0.0000001
T
.
>> format long
>> x=[0.1,0.01,0.001,0.0001,0.00001,0.000001,0.0000001]';
>> sin(x)./x
ans =
0.99833416646828
0.99998333341667
0.99999983333334
0.99999999833333
0.99999999998333
0.99999999999983
1.00000000000000
which suggests that the values approach 1.
We must use ./ to compute the division of a scalar by a vector.
>> format short
>> a=[ 1, 0.1, 0.01, 0.001]
a =
1.0000 0.1000 0.0100 0.0010
>> 1/a
??? Error using ==> /
Matrix dimensions must agree.

>> 1./a
ans =
1 10 100 1000
Vectors 4.11

so we see that 1./a works, but 1/a does not.
Array power (.^)
To square each of the elements of a vector we could, for example, we could perform
the array product a.*a. However, a neater way is to use the .^ operator:
>> a=[2:5]
a =
2 3 4 5
>> a.*a
ans =
4 9 16 25
>> a.^2
ans =
4 9 16 25
We must remember the precedence of the power over multiplication.
>> b=[3:2:9]
b =
3 5 7 9
>> a.*b.^2
ans =
18 75 196 405
Graph plotting
We shall illustrate by considering
( )
1
3
2
9
5
x x
y
x
+ +
=
+

for 0 5 x s s . The plot is shown in Figure 4.1.
>> x=0:0.1:5;
>> y=((9+x).^(1/3)+x)./sqrt(4+x.^2);
>> plot(x,y)
N.B. the repeated use of the ‘dot’ operators but that sqrt does not need a special array
form.
Vectors 4.12

Figure 4.1. Graph of ( )
( )
1
3 2
9 5 y x x x = + + + .