Mod&Sim_Matlab_Lect2_Vectors_Arrays (2)
Mod&Sim_Matlab_Lect2_Vectors_Arrays (2)
Lecture 2
• Vectors in Matlab
• Arrays in Matlab
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.
8
To access the first three elements 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.
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
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].
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.
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
33
34
35
This function changes the dimensions of the array X to the new
size of MxN.
Syntax
36
37
Number of rows and columns of an array can be calculated using “size”
function.
Syntax >> [m,n]=size(X)
38
You can convert an array to a column vector using the colon (:) operator.
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.
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.
44
You can use the colon operator (:) to access a Column in an array.
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,
47
To find the elements in X whose values are 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
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
52
53