Professional Documents
Culture Documents
2
University of Baghdad Matlab Laboratory
College of Engineering Exp. No. 2
Electrical Eng. Dept. 3rd year
Vectors
Introduction
Procedure
Vectors
>> a = [2; 1; 4]
a=
>> c = 3;
>> 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
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
v T = [ v 1 v 2 v n]
>> 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=
>> C = A + B
C=
>> W = [3,0,3];
>> X = [2,1,1];
>> Y = W X
Y=
1 1 2
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:
>> 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
>> T = [R, S]
T=
12 11 9 1 4
x = [xi : q : xe]
>> 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
Column 11
1.0000
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
Column 11
2.7183
>> u = [100:5:80]
u=
100 95 90 85 80
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 =
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.*J
ans =
16
>> a = sum(J.*J)
a=
25
Then the magnitude of the vector is the square root of this quantity:
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
u = [i 12i 4]
i
u u = [i 12i 4] 1 2i = (- i ) ( i ) + (1 - 2 i ) (1 + 2 i ) + (4)(4) = 22
4
u uu = 22
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:
>> sum(u.*u)
ans =
12.
>> v = u'
v=
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
Now we obtain the correct answer, and can get the magnitude:
>> b = sum(v.*u)
b=
22
magu =
4.6904
>> 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.:
B=
2 0 1 9
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:
>> 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:
>> dot(J,J)
ans =
25
mag =
The dot operation works correctly with vectors that contain complex elements:
>> dot(u,u)
ans =
35
>> C = cross(A,B)
C=
1 2 1
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(2)
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
4. Given A = [1; 2; 3]; B = [4; 5; 6];, find the array product of the two vectors.