Week 02: 1 - Vector and Matrices 2 - Graphs and Plots in MATLAB
Week 02: 1 - Vector and Matrices 2 - Graphs and Plots in MATLAB
7 2 10 8 7 17 26 4 3 1 2
3
Scalar 1x9
4 vector
5 1 2 3 4
6 56 6 9 4
3 90 8 10
5x1
vector
3x4
Matrix
All the values stored in these matrices are stored in what are called elements.
2 3 4
3 4 5
>> X(2,3)
ans =
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
DR. MOHAMAD A. ALKHALIDI 16
1 – Vector and Matrices
› Transposing and flipping matrices:
– A common task in linear algebra is to work with the transpose of
a matrix, which turns the rows into columns and the columns
into rows.
› To do this, use the transpose function or the .' operator.
– A = [8 1 6;3 5 7;4 9 2] A=
8 1 6
3 5 7
4 9 2
– B = A .’ >> B = A.'
B=
8 3 4
1 5 9
6 7 2 DR. MOHAMAD A. ALKHALIDI 17
1 – Vector and Matrices
› Changing Dimensions:
– To change the dimensions or configuration of matrices ( or in
many cases vectors), MATLAB has the following famous built-in
functions:
› reshape: changes the dimensions of a matrix,
› flipud: flips the rows of a matrix in an up-to-down direction, and
› fliplr: flips the columns in a left-to-right direction.
› flip: flips any array; it flips a vector (left to right if it is a row vector or up to
down if it is a column vector) or a matrix (up to down by default)
› rot90: rotates the matrix counterclockwise 90 degrees
› repmat: Repeat copies of array.
B= C=
11 18 25 2 9 15 8 1 24 17
10 12 19 21 3 16 14 7 5 23
4 6 13 20 22 22 20 13 6 4
23 5 7 14 16 3 21 19 12 10
17 24 1 8 15 9 2 25 18 11
>> t = [0:2:20]' 50
8 46.9575
10 49.4214
40
>> plot(t,v) 30
12 50.6175
14 51.1871
20
16 51.4560
10 18 51.5823
20 51.6416
0
0 2 4 6 8 10 12 14 16 18 20
DR. MOHAMAD A. ALKHALIDI 32
5 - Graphs & Plots in MATLAB
› Customizing the plot:
– title: Add title to the plot
› Syntax:
– title(‘add text’)
– title(title text, subtitle text): adds a subtitle underneath the title.
– title(___, Name, Value) modifies the title appearance using one or more name-
value pair arguments. For example, 'FontSize',12 sets the font size to 12 points.
Specify name-value pair arguments after all other input arguments. Modifying
the title appearance is not supported for all types of charts.
function:
'|' Vertical line r red
Line Style Description 's' Square g green
- Solid line 'd' Diamond b blue
-- Dashed line '^' Upward-pointing triangle w white
: Dotted line 'v' Downward-pointing triangle k black
-. Dash-dot line '>' Right-pointing triangle
'<' Left-pointing triangle
'p' Pentagram
'h' Hexagram
50 50
40 40
30 30
20 20
10 10
0 0
0 2 4 6 8 10 12 14 16 18 20 0 2 4 6 8 10 12 14 16 18 20
colors: 10
› plot(t, v, '--dc','LineWidth’,2,
'MarkerSize',10,'MarkerEdgeColor','k',' 0
0 2 4 6 8 10 12 14 16 18 20
MarkerFaceColor','m')
50
40
30
20
10
-10
0 2 4 6 8 10 12 14 16 18 20