CCE402: Modeling and Simulation
Lecture 2
• Vectors in Matlab
• Arrays in Matlab
Dr. Sawsan Abdellatif 1
2
Applying the transpose operation to vectors changes a row vector to a column
vector and vice versa.
The Matlab colon
operator, “ : ”, can be
used to convert a vector
to a column vector.
3
The linear method can be used to create a row vector that has linearly spaced
elements, that is, the difference between two successive elements in the vector
is constant.
Logspace (X1,X2,N) generates vector of N logarithmically spaced values between 10𝑋1 and
10𝑋2
4
Two vectors can be concatenated and become a single vector.
5
Applying the transpose operation to a complex vector not only changes rows
to columns and vice versa, but also conjugates the vector’s elements
To change rows to columns and columns to rows only without conjugating the
vector y elements, the command y.’ can be applied
6
(x>y) command determines whether
the value of each element in the
vector x is greater than the
corresponding element in the vector
y. The result is saved in z vector.
Remember: An input to relational and logical operators is
considered to be true if it has a nonzero value.
7
To access the third element in the vector,
To access the last element in the vector,
Try to access the 7th element in vector x:
8
To access the first three elements of the vector y
To access the last three element of the vector y
To access the 2nd , 3rd , and the 4th elements of the vector y
or
To access the 2nd , 4th , and the 6th elements of the vector y
9
To find the indices of the elements whose values are equal to 5,
“==” sign checks whether the values of the elements in the vector y are equal to 5.
To find the indices of the elements in the vector y whose values are greater than 7,
To find the indices of the elements in the vector y whose values are less than or
equal to 9,
To find the values of the elements in y whose values are less than or equal to 9,
10
Matlab has an interesting way of using the relational and logical
operations to access elements in vectors.
The command y(x>3) here outputs elements of y that correspond to the
same positions where x is greater than 3.
11
Ex:
12
The addition and subtraction of two vectors is performed on an element-
by-element basis.
The vectors must be of equal dimensions.
add value to all elements in the vector x Sub value from all elements in the vector x
13
Note matrix multiplication condition:
Number of columns of the 1st vector (x)
must equal number of rows of the 2nd
vector (y)
14
The multiplication process here produces a single value scalar number.
>> y*x
The multiplication process here produces a square matrix.
15
Matrix division of vectors does not
have any mathematical meaning.
16
% most frequently used value
17
If we need to plot the function y = x^2, where x is in the range [ - 3, 3].
Plot (x,y) draws points of y w.r.t
points of x vector and connects the
points together using straight lines.
Note: the 1st argument is the
horizontal axis and the 2nd argument
is the vertical axis
18
To improve the resolution of the plot, you need to increase the number
of points for the x vector
19
Matlab support the colors:
red “r”,
green “g”,
blue “b”,
cyan “c”,
magenta “m”,
yellow “y”,
white “w”
black “k”.
20
Different symbols can be used
to represent points in a curve.
e.g., “+”, “o” or “x”.
For more information: use help
21
22
23
Here, the added text starts at the plot
coordinate location (1, 0.75) on the
figure.
We can set the font size for the axes
labels, the figure title, and any text added
to the figure.
24
25
Note: ‘Location’ parameter sets the location of the legend on the figure:
e.g.,
‘SouthEast’, ‘SouthWest’, ‘NorthEast’, ‘NorthWest’
26
27
1 2
subplot(2,2,1) subplot(2,2,2)
3 4
subplot(2,2,3) subplot(2,2,4)
28
29
bar stairs
stems
30
31
32
Syntax
This function creates a large matrix B consisting of an MxN copies
of A.
This function has the following three arguments:
33
34
35
This function changes the dimensions of the array X to the new
size of MxN.
Syntax
The elements are taken from the source array X in a column-by-
column fashion.
36
37
Number of rows and columns of an array can be calculated using “size”
function.
Syntax >> [m,n]=size(X)
m is number of rows, n is number of columns
To find the total number of elements in the array X:
38
You can convert an array to a column vector using the colon (:) operator.
For example, to convert the array X to a column vector, type:
Note that the elements have been extracted from the array X, in a column-by-
column fashion.
39
Arrays can be concatenated (combined) together to produce larger arrays.
Note that here we have used the semicolon (;) to combine Z and X arrays
in the vertical direction.
40
Note that here we have used the comma (,) to combine X and R arrays in
the horizontal direction.
41
Two methods to access the elements within an array:
Row-and-column indexing.
Linear indexing.
An element in the array X is referred to as Xm,n , where m
refers to the row number and n refers to the column number.
To access the last element in the first row of X,
To access the last element in the third column of X,
42
>>a=X(1)
a=
3
>>b=X(7)
b=
8
>>c=X(12) >>c=X(end)
c= c=
10 10
43
You can use the colon operator (:) to access a Row in an array.
To access the first row:
To access the last row,
To access the last two rows
To access the first and the third rows,
44
You can use the colon operator (:) to access a Column in an array.
To access the first column:
To access the last column,
To access the first and second columns,
45
To access the first and second rows of the third column.
To access the second, third, and fourth columns of the second row
To access the elements that are encircled by the rectangular area below:
46
To find the indices of the elements whose values is
greater than 7,
This output means that the elements E(3,2) and E(1,3) are greater than 7.
To find the indices of the elements in the array E whose value is less than
3,
To find the values of the elements in E that are less than 3,
47
To find the elements in X whose values are greater than
3.
To access the elements of Y which have the corresponding positions
within the array to locations in the array X where X is greater than 3.
48
commands description
mean2 mean of matrix elements
std2 Standard deviation of matrix elements
det Matrix Determinant
inv Matrix inverse.
diag Matrix diagonal
trace Sum of diagonal elements
max(max(A)) Maximum element in matrix A
min(min(A)) Minimum element in matrix A
zeros Create zeros array.
ones Create ones array.
eye Create Identity matrix.
rand Create array with random numbers between 0 and 1 49
Let us plot the function Z = 𝑋 2 − 𝑌 2
where X is in the range of [-2, 2] and Y is in the range of [-3, 3].
50
To improve the resolution of the 3D
plot, increase the number of points
within the X and Y arrays
51
The surf function plots an array as a surface
The relationship between the color in the
surface plot and the value of Z can be
shown using colorbar command
52
53