You are on page 1of 17

Experiment

2
University of Baghdad Matlab Laboratory
College of Engineering Exp. No. 2
Electrical Eng. Dept. 3rd year

Vectors
Introduction

One area where MATLAB is particularly useful is in the computer


implementation of linear algebra problems. This is because MATLAB has exceptional
capabilities for handling arrays of numbers, making it a useful tool for many scientific
and engineering applications.

Procedure

Vectors

A vector is a one-dimensional array of numbers. MATLAB allows you to


create column vectors or row vectors. A column vector can be created in MATLAB
by enclosing a set of semicolon delimited numbers in square brackets. Vectors can
have any number of elements. For example, to create a column vector with three
elements we write:

>> a = [2; 1; 4]

a=

Eng. Ahmed M. Abdulhussain


University of Baghdad Matlab Laboratory
College of Engineering Exp. No. 2
Electrical Eng. Dept. 3rd year

Basic operations on column vectors can be executed by referencing the


variable name used to create them. If we multiply a column vector by a number, this
is called scalar multiplication. Suppose that we wanted to create a new vector such
that its components were three times the components in the vector a we just created
above. We could start by defining a scalar variable (remember that a semicolon after a
command suppresses the output):

>> c = 3;

Next, we just perform the operation treating a like another variable:

>> b = c*a

b=

12

To create a row vector, we enclose a set of numbers in square brackets but this
time use a space or comma to delimit the numbers. For example:

>> v = [2 0 4]

v=

2 0 4

or using commas:

>> w = [1,1,9]

w=

1 1 9

Eng. Ahmed M. Abdulhussain


University of Baghdad Matlab Laboratory
College of Engineering Exp. No. 2
Electrical Eng. Dept. 3rd year

Column vectors can be turned into row vectors and vice versa using the
transpose operation. Suppose that we have a column vector with n elements denoted
by:

v1
v
2
.

v=.
.

.
vn

Then the transpose is given by:

v T = [ v 1 v 2 v n]

In MATLAB, we represent the transpose operation with a single quote or tick


mark (). Taking the transpose of a column vector produces a row vector:

>> a = [2; 1; 4];

>> y = a'

y=

2 1 4

Now lets take the transpose of a row vector to produce a column vector:

>> Q = [2 1 3]

Q=

2 1 3

>> R = Q'
Eng. Ahmed M. Abdulhussain
University of Baghdad Matlab Laboratory
College of Engineering Exp. No. 2
Electrical Eng. Dept. 3rd year

R=

It is also possible to add or subtract two vectors to produce another. In order to


perform this operation the vectors must both be of the same type and the same length,
so we can add two column vectors together to produce a new column vector or we can
add two row vectors to produce a new row vector, for example. This can be done
referencing the variables only, it is not necessary for the user to list the components.
For example, lets add two column vectors together:

>> A = [1; 4; 5];

>> B = [2; 3; 3];

>> C = A + B

C=

Now lets subtract one row vector from another:

>> W = [3,0,3];

>> X = [2,1,1];

>> Y = W X

Y=

1 1 2

Eng. Ahmed M. Abdulhussain


University of Baghdad Matlab Laboratory
College of Engineering Exp. No. 2
Electrical Eng. Dept. 3rd year

Creating Larger Vectors from Existing Variables

MATLAB allows you to append vectors together to create new ones. Let u and
v be two column vectors with m and n elements respectively that we have created in
MATLAB. We can create a third vector w whose first m elements are the elements of
u and whose next n elements are the elements of v. The newly created column vector
has m + n elements. This is done by writing w = [u; v]. For example:

>> A = [1; 4; 5];

>> B = [2; 3; 3];

>> D = [A;B]

D=

This can also be done using row vectors. To create a row vector u with m + n
elements from a vector r with m elements and a vector s with n elements, we write

u = [r, s]. For example:

>> R = [12, 11, 9];

>> S = [1, 4];


Eng. Ahmed M. Abdulhussain
University of Baghdad Matlab Laboratory
College of Engineering Exp. No. 2
Electrical Eng. Dept. 3rd year

>> T = [R, S]

T=

12 11 9 1 4

Creating Vectors with Uniformly Spaced Elements

It is possible to create a vector with elements that are uniformly spaced by an


increment q, where q is any real number. To create a vector x with uniformly spaced
elements where xi is the first element and xe is the final element, the syntax is:

x = [xi : q : xe]

For example, we can create a list of even numbers from 0 to 10 by writing:

>> x = [0 : 2 : 10]

x=

0 2 4 6 8 10

We have already been using this technique to create a list of values for plotting
purposes. Lets see how this works behind the scenes by looking at some vectors with
a small number of elements. First we create a set of x values:

>> x = [0:0.1:1]

x=

Columns 1 through 10

0 0.1000 0.2000 0.3000 0.4000 0.5000

0.6000 0.7000 0.8000 0.9000

Column 11

1.0000

Eng. Ahmed M. Abdulhussain


University of Baghdad Matlab Laboratory
College of Engineering Exp. No. 2
Electrical Eng. Dept. 3rd year

The set of x values can be used to create a list of points representing the values
of some given function. For example, suppose that y = ex. Then we have:

>> y = exp(x)

y=

Columns 1 through 10

1.0000 1.1052 1.2214 1.3499 1.4918 1.6487

1.8221 2.0138 2.2255 2.4596

Column 11

2.7183

Returning to the process of creating an array of uniformly spaced elements, be


aware that you can also use a negative increment. For instance, lets create a list of
numbers from 100 to 80 decreasing by 5:

>> u = [100:5:80]

u=

100 95 90 85 80

Eng. Ahmed M. Abdulhussain


University of Baghdad Matlab Laboratory
College of Engineering Exp. No. 2
Electrical Eng. Dept. 3rd year

Characterizing a Vector

The length command returns the number of elements that a vector contains.
For example:

>> A = [2;3;3;4;5];

>> length(A)

ans =

>> B = [1;1];

>> length(B)

ans =

The length command can be applied to row and column vectors as we will see
below, We can find the largest and smallest elements in a vector using the max and
min commands. For example:

>> A = [8 4 4 1 7 11 2 0];

>> max(A)

ans =

11

>> min(A)

ans =

Eng. Ahmed M. Abdulhussain


University of Baghdad Matlab Laboratory
College of Engineering Exp. No. 2
Electrical Eng. Dept. 3rd year

To find the magnitude of a vector we can employ two operations. Recall that
the magnitude of a vector

v1
v
2
.

v=.
.

.
vn

is given by:

2
v = v 12 + v 22 + + v 2

To perform this operation, we will first take the dot product of a vector with
itself. This is done by using array multiplication (.*). First lets define a vector:

>> J = [0; 3; 4];

Now we can do array multiplication:

>> J.*J

ans =

16

This produces a vector whose elements are 12 , 22 , To get the summation we


need, we can use the sum operator:

Eng. Ahmed M. Abdulhussain


University of Baghdad Matlab Laboratory
College of Engineering Exp. No. 2
Electrical Eng. Dept. 3rd year

>> a = sum(J.*J)

a=

25

Then the magnitude of the vector is the square root of this quantity:

>> mag = sqrt(a)

mag =

If a vector contains complex numbers, then more care must be taken when
computing the magnitude. When computing the row vector, we must compute the
complex conjugate transpose of the original vector. For example, if:

i
u 1 2i

4

Then to compute the magnitude, we need the following vector:

u = [i 12i 4]

Then the summation we need to compute is:

i
u u = [i 12i 4] 1 2i = (- i ) ( i ) + (1 - 2 i ) (1 + 2 i ) + (4)(4) = 22

4

Hence the magnitude of a vector with complex elements is:

u uu = 22

Eng. Ahmed M. Abdulhussain


University of Baghdad Matlab Laboratory
College of Engineering Exp. No. 2
Electrical Eng. Dept. 3rd year

You can see how using the complex conjugate transpose when computing our
sum ensures that the magnitude of the vector will be real. Now lets see how to do this
in MATLAB. First lets enter this column vector:

>> u = [i; 1 + 2i; 4];

If we just compute the sum of the vector multiplication as we did in the


previous example, we get a complex number that wont work:

>> sum(u.*u)

ans =

12.

So lets define another vector which is the complex conjugate transpose of u.


MATLAB does this automatically with the transpose operator:

>> v = u'

v=

0 1.0000i 1.0000 2.0000i 4.0000

Lets just compute the complex conjugate of the vector, and form the sum. We
can get the complex conjugate of a vector with the conj command:

>> v = conj(u)

v=

0 1.0000i

1.0000 2.0000i

4.0000

Eng. Ahmed M. Abdulhussain


University of Baghdad Matlab Laboratory
College of Engineering Exp. No. 2
Electrical Eng. Dept. 3rd year

Now we obtain the correct answer, and can get the magnitude:

>> b = sum(v.*u)

b=

22

>> magu = sqrt(b)

magu =

4.6904

Of course this could all be done in one step by writing:

>> c = sqrt(sum(conj(u).*u))

c=

4.6904

We can use the abs command to return the absolute value of a vector, which is
a vector whose elements are the absolute values of the elements in the original vector,
i.e.:

>> A = [2 0 1 9] >> B = abs(A)

B=

2 0 1 9

Eng. Ahmed M. Abdulhussain


University of Baghdad Matlab Laboratory
College of Engineering Exp. No. 2
Electrical Eng. Dept. 3rd year

Vector Dot Products

The dot product between two vectors A = (a1 a2 an) and B = (b1 b2 bn) is
given by

A.B = a b
i
i i

In MATLAB, the dot product of two vectors a, b can be calculated using the
dot(a,b) command.

The dot product between two vectors is a scalar, i.e. its just a number. Lets
compute a simple example using MATLAB:

>> a = [1;4;7]; b = [2;1;5];

>> c = dot(a,b)

c=

33

The dot product can be used to calculate the magnitude of a vector. All that
needs to be done is to pass the same vector to both arguments. Consider the vector in
the last section:

>> J = [0; 3; 4];

Calling dot we obtain:

>> dot(J,J)

ans =

25

Eng. Ahmed M. Abdulhussain


University of Baghdad Matlab Laboratory
College of Engineering Exp. No. 2
Electrical Eng. Dept. 3rd year

Or we can calculate the magnitude of the vector this way:

>> mag = sqrt(dot(J,J))

mag =

The dot operation works correctly with vectors that contain complex elements:

>> u = [i; 1 + i; 4 + 4*i];

>> dot(u,u)

ans =

35

Another important operation involving vectors is the cross product. To


compute the cross product, the vectors must be three dimensional. For example:

>> A = [1 2 3]; B = [2 3 4];

>> C = cross(A,B)

C=

1 2 1

Referencing Vector Components

MATLAB has several techniques that can be used to reference one or more of
the components of a vector. The ith component of a vector v can be referenced by
writing v(i). For example:

>> A = [12; 17; 2; 0; 4; 4; 11; 19; 27];

>> A(2)

Eng. Ahmed M. Abdulhussain


University of Baghdad Matlab Laboratory
College of Engineering Exp. No. 2
Electrical Eng. Dept. 3rd year

ans =

17

>> A(8)

ans =

19

Referencing the vector with a colon, such as v(:); tells MATLAB to list all of
the components of the vector:

>> A(:)

ans =

12

17

11

19

27

We can also pick out a range of elements out of a vector. The example weve
been working with so far in this section is a vector A with nine components. We can
reference components four to six by writing A(4:6) and use these to create a new
vector with three components:

>> v = A(4:6)
Eng. Ahmed M. Abdulhussain
University of Baghdad Matlab Laboratory
College of Engineering Exp. No. 2
Electrical Eng. Dept. 3rd year

v=

In the next section, we will see how these techniques can be used to reference
the columns or rows in an array of numbers called a matrix.

Report

1. Find the magnitude of the vector A = (1 7 3 2) by two method.

2. Find the magnitude of the vector A = (1+i 7i 3 22i) by two method.

3. Consider the numbers 1, 2, 3, 4 . Enter these as components of a column


vector and as components of a row vector and find the transpose operation of
the vector.

4. Given A = [1; 2; 3]; B = [4; 5; 6];, find the array product of the two vectors.

Eng. Ahmed M. Abdulhussain

You might also like