Lecture 4, Vectors and Matrices-1
Lecture 4, Vectors and Matrices-1
The elements of the matrix are denoted 𝑎𝑖𝑗 , where 𝑖 indicates the row number and 𝑗 the column number.
In MATLAB, the order of the matrix is referred to as its size. Several special cases of this general matrix
are as follows:
Square Matrix
Diagonal matrix
When 𝑎𝑖𝑗 = 𝑎𝑖1 , that is, there is only one column, then is called a column matrix or, more commonly, a
vector, that is,
ES106: Programming for Engineers, Lecture Note, Prof. (Dr.) Vinay J. Patel
𝑎11 𝑎1
𝑎21 𝑎2
𝑎 = [ ⋮ ] = [ ⋮ ] → (𝑚 × 1)
𝑎𝑚1 𝑎𝑚
When 𝑎𝑖𝑗 = 𝑎1𝑗 , that is, there is only one row, then is called a row matrix or a vector, that is,
This is the default definition of a vector in MATLAB. In this case, 𝑛 is the length of the vector. Thus, vector
can be represented by a row or a column matrix.
and if
𝑎1
𝑎2
𝑎 = [𝑎1 𝑎2 ⋯ 𝑎𝑛 ] → (1 × 𝑛) then 𝑎′ = [ ] → (𝑛 × 1)
⋮
𝑎𝑛
CREATION OF VECTORS
Vectors in MATLAB are expressed as either
𝑥 = [𝑎 𝑝 𝑧] 𝑜𝑟 𝑥 = [𝑎, 𝑝, 𝑧]
ES106: Programming for Engineers, Lecture Note, Prof. (Dr.) Vinay J. Patel
where 𝑎, 𝑝, 𝑧, ⋯ are either variable names, numbers, expressions, or strings. If they are variable names or
expressions, then all variable names and the variable names composing the expressions must be defined
such that a numerical value has been obtained for each of these variable names prior to the execution of
this statement. Variable names, expressions, and numbers can appear in any combination and in any
order. In the form
𝑥 = [𝑎 𝑝 𝑧]
𝑥 = [𝑎, 𝑝, 𝑧]
the blank is optional.
Colon Notation
MATLAB gives several additional ways to assign numerical values to the elements of a vector. The first
method uses the colon notation to specify the range of the values and the increment between adjacent
values. The second method specifies the range of the values and the number of values desired. In the first
method, the increment is either important or has been specified, whereas in the second method, the
number of values is important.
𝑥 = 𝑠: 𝑑: 𝑓 𝑜𝑟 𝑥 = (𝑥: 𝑑: 𝑓) 𝑜𝑟 𝑥 = [𝑠: 𝑑: 𝑓]
where, 𝑠 = start or initial value, 𝑑 = increment or decrement, 𝑓 = end or final value. If 𝑑 is not specified
then value 1 is considered as increment. With any of these command, the following row vector 𝑥 is
created.
Example:
Let
𝑏 = [𝑏1 𝑏2 ⋯ 𝑏𝑛 ]
This means that we have created a vector that has one row and columns. In order to access individual
elements of this vector, we use MATLAB’s subscript notation. This notation has the form 𝑏(𝑗), that is 𝑏(𝑗),
ES106: Programming for Engineers, Lecture Note, Prof. (Dr.) Vinay J. Patel
corresponds to 𝑏𝑗 , where 𝑗 = 1,2, ⋯ 𝑛 is the 𝑗th location in the vector 𝑏. Thus, if we write 𝑏(3), then
MATLAB will return 𝑏3 the numerical value assigned to , the third element of the vector. However,
MATLAB’s interpreter is smart enough to know that the matrix 𝑏 is a (1 × 𝑛) matrix, and in some sense,
it ignores the double subscript requirement. That is, writing 𝑏(3), where is a vector defined above, is the
same as writing it as 𝑏(1,3). However, if one were to either directly or implicitly require 𝑏(3,1), then an
error message would appear because this row (the third row) hasn’t been defined (created).
Conversely, if we let
𝑏 = [𝑏1 𝑏2 ⋯ 𝑏𝑛 ]′
we have created a column vector, that is, a (𝑛 × 1) matrix. If we want to locate the third element of this
vector, then we again write 𝑏(3) and MATLAB returns the numerical value corresponding to 𝑏3 . This is
the same as having written 𝑏(3,1). If one were to either directly or implicitly require 𝑏(1,3), then an error
message would appear because this column (the third column) hasn’t been defined (created).
The elements of a vector can also be accessed using subscript colon notation. In this case, the subscript
colon notation is a shorthand method of accessing a group of elements. For example, for a vector with
elements, one can access a group of them using the notation
𝑏(𝑘: 𝑑: 𝑚)
where 1 ≤ 𝑘 < 𝑚 ≤ 𝑛, 𝑘 and 𝑚 are positive integers and, in this case 𝑑 is a positive integer. Thus if one
has ten elements in a vector 𝑏, the third through seventh elements are selected by using
𝑏(3: 7)
x = [-2,1:2:9, 10];
xlast=
xlast = x(end)
10
x = [-2,1:2:9, 10]; z=
z = x-1 -3 0 2 4 6 8 9
x = [-2,1:2:9, 10];
x=
x(2) = (2)/2;
x -2.000 0.5000 3.0000 5.0000 7.0000 9.0000 10.0000
y = [-1 6 15 -7 31 2 -
x=
4, -5];
x = y(3:5) 15 -7 31
y = [-1 6 15 -7 31 2 -
x=
4, -5];
x = [y(1) y(2) y(7)] -1 6 -4
y = [-1 6 15 -7 31 2 -
x=
4, -5];
index = [1 2 8]; -1 6 -4
ES106: Programming for Engineers, Lecture Note, Prof. (Dr.) Vinay J. Patel
x = y(index)
y = [-1 6 15 -7 31 2 -
x=
4, -5];
x = y([1 2 8]) -1 6 -4
x = linespace(-pi, pi, y =
10); -0.0000 -0.6428 -0.8660 -0.3420 0.3420 0.8550 0.9848 0.6428
y = sin(x) 0.0000
CREATION OF MATRICES
Consider the following (4 × 3) matrix:
𝑎11 𝑎12 𝑎13
𝑎21 𝑎22 𝑎23
𝐴 = [𝑎 𝑎32 𝑎33 ] → (4 × 3)
31
𝑎41 𝑎42 𝑎43
This matrix can be created several ways, the basic syntax to create a matric is
𝐴 = [𝑎11 𝑎12 𝑎13 ; 𝑎21 𝑎22 𝑎23 ; 𝑎31 𝑎32 𝑎33 ; 𝑎41 𝑎42 𝑎43 ]
where the semicolons are used to indicate the end of a row. Each row must have the same number of
columns. If a more readable presentation is desired, then one can use the form
where the ellipses (⋯ )are required to indicate that the expression continues on the next line. One can
omit the ellipsis (⋯ ) and instead use the Enter key to indicate the end of a row. In this case, the expression
will look like
A fourth way is to create four separate row vectors, each with the same number of columns, and then
combine these vectors to form the matrix. In this case, we have
ES106: Programming for Engineers, Lecture Note, Prof. (Dr.) Vinay J. Patel
𝑣4 = [𝑎41 𝑎42 𝑎43 ];
𝐴 = [𝑣1 ; 𝑣2 ; 𝑣3 ; 𝑣4 ]
where the semicolons in the first four lines are used to suppress display to the command window. The
fourth form is infrequently used.
In all the forms above, the 𝑎𝑖𝑗 are numbers, variable names, expressions, or strings. If they are either
variable names or expressions, then the variable names or the variable names comprising the expressions
must have been assigned numerical values, either by the user or from previously executed expressions,
prior to the execution of this statement. Expressions and numbers can appear in any combination. If they
are strings, then the number of characters in each row must be the same.
A = [11:14;21:24;31:34;41:44]
A=
A = [11,12,13,14;… 11 12 13 14
21,22,23,24;…
21 22 23 24
31,32,33,34;…
41,42,43,44] 31 32 33 34
v1 = [11,12,13,14]; 41 42 43 44
v2 = [21,22,23,24];
v3 = [31,32,33,34];
v4 = [41,42,43,44];
A = [v1; v2; v3; v4];
The order of matrix is determined by
[𝑚, 𝑛] = 𝑠𝑖𝑧𝑒(𝐴)
where 𝑚 is the number of rows and is 𝑛 the number of columns. If one were to use the 𝑙𝑒𝑛𝑔𝑡ℎ function
for a matrix quantity, 𝑙𝑒𝑛𝑔𝑡ℎ would return the number of columns in the array. The transpose of a matrix
is obtained by using the apostrophe (′).
ES106: Programming for Engineers, Lecture Note, Prof. (Dr.) Vinay J. Patel
Generation of special matrix
A=
and
ES106: Programming for Engineers, Lecture Note, Prof. (Dr.) Vinay J. Patel
𝑏11 𝑏12 𝑏13
𝐵=[ ]
𝑏21 𝑏22 𝑏23
C=
C = A±B % Addition or Subtraction 𝑎11 ± 𝑏11 𝑎12 ± 𝑏12 𝑎13 ± 𝑏13
𝑎21 ± 𝑏21 𝑎22 ± 𝑏22 𝑎23 ± 𝑏23
C=
𝑎11 𝑎12 𝑎13 𝑏11 𝑏12 𝑏13
C = [A,B] % Column Augmentation 𝑎21 𝑎22 𝑎23 𝑏21 𝑏22 𝑏23
C=
𝑎11 𝑎12 𝑎13
C = [A;B] % Row Augmentation 𝑎21 𝑎22 𝑎23
𝑏11 𝑏12 𝑏13
𝑏21 𝑏22 𝑏23
Dot Operator
(.) followed by any arithmetic operator (+,-,*,/,^) performs element by element arithmetic
operation. If we consider
𝑎11 𝑎12 𝑎13
𝐴 = [𝑎 𝑎22 𝑎23 ]
21
and
𝑏11 𝑏12 𝑏13
𝐵=[ ]
𝑏21 𝑏22 𝑏23
C=
C = A.*B % Element multiplication 𝑎11 ∗ 𝑏11 𝑎12 ∗ 𝑏12 𝑎13 ∗ 𝑏13
𝑎21 ∗ 𝑏21 𝑎22 ∗ 𝑏22 𝑎23 ∗ 𝑏23
C=
C = A/B % Element Division 𝑎11 /𝑏11 𝑎12 /𝑏12 𝑎13 /𝑏13
𝑎21 /𝑏21 𝑎22 /𝑏22 𝑎23 /𝑏23
C=
C = A^B % Row Augmentation 𝑎11 ^𝑏11 𝑎12 ^𝑏12 𝑎13 ^𝑏13
𝑎21 ^𝑏21 𝑎22 ^𝑏22 𝑎23 ^𝑏23
where
𝑘
and is of order (𝑚 × 𝑛). Notice that the product of two matrices is defined only when the adjacent
integers of their respective orders are equal; 𝑘 in this case. In other words, (𝑚 × 𝑘)(𝑘 × 𝑛), where the
notation indicates that we have summed terms as indicated in Eq. The MATLAB expression for matrix
multiplication is
𝐶 =𝐴∗𝐵
Example 1:
Solution:
The transformation from polar coordinates to Cartesian coordinates can be calculated as shown in Figure
1.
ES106: Programming for Engineers, Lecture Note, Prof. (Dr.) Vinay J. Patel
that is
𝑥 = cos 𝜃
𝑦 = sin 𝜃
Hence,
𝑟1
𝑟2
𝑋 = 𝑟′ ∗ cos(𝜃) = [ ⋮ ] [𝜃1 𝜃2 ⋯ 𝜃𝑛 ]
𝑟𝑚
𝑟1 cos 𝜃1 𝑟1 cos 𝜃2 ⋯ 𝑟1 cos 𝜃𝑛
𝑟 cos 𝜃1 𝑟2 cos 𝜃2 ⋯ 𝑟2 cos 𝜃𝑛
=[ 2 ]
⋮ ⋮ ⋱ ⋮
𝑟𝑚 cos 𝜃1 𝑟𝑚 cos 𝜃2 ⋯ 𝑟𝑚 cos 𝜃𝑛
and
𝑟1
𝑟2
𝑋 = 𝑟′ ∗ sin(𝜃) = [ ⋮ ] [𝜃1 𝜃2 ⋯ 𝜃𝑛 ]
𝑟𝑚
𝑟1 sin 𝜃1 𝑟1 sin 𝜃2 ⋯ 𝑟1 sin 𝜃𝑛
𝑟 sin 𝜃1 𝑟2 sin 𝜃2 ⋯ 𝑟2 sin 𝜃𝑛
=[ 2 ]
⋮ ⋮ ⋱ ⋮
𝑟𝑚 sin 𝜃1 𝑟𝑚 sin 𝜃2 ⋯ 𝑟𝑚 sin 𝜃𝑛
Thus, we have mapped the polar coordinates into their Cartesian counterparts. This procedure is very
useful in plotting results.
Determinants
For 𝑛 = 2
For 𝑛 = 3
|𝐴| = 𝑎11 𝑎22 𝑎33 + 𝑎12 𝑎23 𝑎31 + 𝑎13 𝑎21 𝑎32 − 𝑎13 𝑎22 𝑎31 − 𝑎11 𝑎23 𝑎32 − 𝑎12 𝑎21 𝑎33
Matric Inverse
𝐴−1 𝐴 = 𝐴𝐴−1 = 𝐼
provided that 𝐴 is not singular, that is, its determinant is not equal to zero(|𝐴| ≠ 0). The quantity 𝐼 is the
identity matrix. The superscript “-1” denotes the inverse. The expression for obtaining the inverse of
matrix A is either
inv(A)
or
A^-1
1
It is important to note that 𝐴 ≠ 𝐴−1 ; 1/𝐴 will cause the system to respond with an error message.
However, 1./𝐴 is a valid expression, but it is not, in general, equal to 𝐴−1 .
𝐴𝑥 = 𝑏
where 𝐴 is (𝑛 × 𝑛) matrix
𝑎11 𝑎12 ⋯ 𝑎1𝑛
𝑎21 𝑎22 ⋯ 𝑎2𝑛
𝐴=[ ⋮ ⋮ ⋱ ⋮ ] → (𝑛 × 𝑛)
𝑎𝑛1 𝑎𝑛2 ⋯ 𝑎𝑛𝑛
ES106: Programming for Engineers, Lecture Note, Prof. (Dr.) Vinay J. Patel
𝐴−1 𝐴𝑥 = 𝐴−1 𝑏
𝑥 = 𝐴−1 𝑏
since 𝐴−1 𝐴 = 𝐼, the identity matrix, and 𝐼𝑥 = 𝑥. The preferred expression for solving this system of
equations is
x = A\b
or
x = linsolve(A,b)
where the backslash operator indicates matrix division and is referred to by MATLAB as left matrix
divide. Left division uses a procedure that is more numerically stable when compared to the methods
used for either of the following alternative notations:
x = A^-1*b
or
x = inv(A)*b
For large matrices, these two alternative expressions execute considerably slower than when the
backslash operator is used.
Table 1List of function introduced in this note
ES106: Programming for Engineers, Lecture Note, Prof. (Dr.) Vinay J. Patel
Exercises:
Write MATLAB code/s for the following:
1. Create two vectors, on whose elements are 𝑎𝑛 = 2𝑛 − 1 and other whose elements are 𝑏𝑛 =
2𝑛 + 1, 𝑛 = 0,1, … , 𝑛. Determine the following:
a. 𝑎 + 𝑏
b. 𝑎 − 𝑏
c. 𝑎’𝑏
d. determinant of 𝑎’𝑏
e. ab’
2.
a. Create a vector of eight values that are equally spaced on a logarithm scale. The first value of the
vector is 6 and the last value is 106.
b. Display the value of the fifth element of the vector created in (a).
c. Create a new vector whose elements are the first, third, fifth, and seventh elements of the vector
created in (a).
3. Let z = magic(5)
a. Perform the following operations to 𝑧 in the order given:
i. Divide column 2 by √3.
ii. Add the elements of the third row to the elements in the fifth row (the third row remains
unchanged).
iii. Multiply the elements of the first column by the corresponding elements of the fourth
column and place the result in the first column.
iv. Set the diagonal elements to 2.
b. If the result obtained in (a) (iv) is denoted as 𝑞, then obtain the diagonal of 𝑞𝑞’.
c. Determine the maximum and minimum values of 𝑞.
4. A matrix is an orthogonal matrix if
𝑋′𝑋 = 𝐼
and, therefore (𝑋 ′ 𝑋)−1 = 𝐼. Show that each of the following matrices is orthogonal
−1 −1 1 −1 −1 1
1 1 −1 1 1 1 −1 −1
𝑤= [ ] 𝑞= [ ]
2 −1 1 2 1 −1 1 −1
1 1 1 1 1 1
5. Given the following matrix:
2 −2 −4
𝐴 = [−1 3 4]
1 −2 −2
Show that 𝐴2 − 𝐴 = 0.
6. If
1 2 2
𝐴 = [2 1 2]
2 2 1
Show that 𝐴2 − 4𝐴 − 5𝐼 = 0, where 𝐼 is the identity matrix.
7. If
1 −1 1 1
𝐴=[ ] 𝑎𝑛𝑑 𝐵 = [ ]
2 −1 4 −1
ES106: Programming for Engineers, Lecture Note, Prof. (Dr.) Vinay J. Patel
Show that (𝐴 + 𝐵)2 = 𝐴2 + 𝐵2 .
1 3 4 11 34 6
𝐴 = [31 67 9] 𝑎𝑛𝑑 𝐵 = [ 7 13 8 ]
7 5 9 43 10 53
ES106: Programming for Engineers, Lecture Note, Prof. (Dr.) Vinay J. Patel