Professional Documents
Culture Documents
Vectors Matrices
Vectors Matrices
Vectors Matrices
1
Scalar, Vector and Matrix
• Matlab is written to work with matrices.
• Matrices are used to store sets of values
• A matrix can be visualised as a table of values.
arrays
3 7 5 1 3
7 3 6
2 4 9 3 2 9
8 0 4 6 row vector 5 scalar
3 x 4 matrix Column vector
3
Creating Row Vector
Several ways;
a) Put the values of the vector in square brackets
separate by space or coma
>> load=[3 7 5 4]
load =
3 7 5 4
>> dist=[1,3,4,6]
dist =
1 3 4 6
4
Creating Row Vector – cont’d
b) Use colon operator for vector with constant
spacing by specifying the first term, the
spacing, and the last term.
>> load=[1:3:10]
load =
With [ ]
1 4 7 10
>> load=1:3:15
load = Without [ ]
1 4 7 10 13
>> dist=3:7
dist = Default step 1
3 4 5 6 7 5
Creating Row Vector – cont’d
c) Use linspace function for vector with constant
spacing by specifying the first term, the last term,
and the number of terms.
>> dist=linspace(0,14,4)
dist =
0 4.6667 9.3333 14.0000
>>vec=[2 1 5 4 3 6];
>> vec(3)
ans = Single element
5
8
Creating Column Vector
To create column vector;
a) Put the values of the vector in square brackets
separate by semicolon (;) or pressing Enter key
after each element typed.
Enter key
semicolon >> disp=[3
>> disp=[3;1;5;4] 5
disp = 6]
3 disp =
1 3
5 5
4 6
9
Creating Column Vector – cont’d
b) By transposing row vector
10
Matrices
Creating
Modifying
Special Matrices
11
Creating Matrix Variables
It is just a generalisation of creating row and column
vector variables;
• Values within row are separated by either space
or coma.
• Different rows are separated by semicolon or
pressing Enter key.
semicolon Enter key
>> K=[5 4 8;3 9 1] >> k2=[1 5 3
K = 6 4 8]
5 4 8 k2 =
3 9 1 1 5 3
6 4 8
13
Try This !
Think what would be produced by the following sequence of
statements and expressions. (Parts (b), (c), and (d) use the vector
that was defined in part (a).)
(a) m=9:-2:1 (b) m2=[m m] (c) n=[m;m] (d) p=[m’ m’]
>> m=9:-2:1
m = >> p=[m' m']
9 7 5 3 1 p =
>> m2=[m m] 9 9
m2 = 7 7
9 7 5 3 1 9 7 5 3 1 5 5
>> n=[m;m] 3 3
n = 1 1
9 7 5 3 1
9 7 5 3 1
14
Matrix Using rand Function
Square
>> rand(2)
ans =
0.1576 0.9572
0.9706 0.4854
rectangular
>> rand(3,2)
ans =
0.6557 0.9340
0.0357 0.6787
0.8491 0.7577
15
Matrix Using randi Function
b) matrix 10 10 10
10 10 10
18
Referring to Matrix Elements
To refer to matrix elements, the row and column are
typed in the brackets.
>> k=[2:6;3:2:11]
k =
Create matrix k
2 3 4 5 6
3 5 7 9 11
>> k(2,4)
Extracting single element:
ans = Row 2, col 4
9
21
Modifying Matrix Elements
• Individual element in a matrix can be modified by
assigning value.
>> mat=[2:5;4:7];
>> mat(2,3)=10
mat = Modify single element
2 3 4 5
4 5 10 7
>> mat(1,:)=6:9
mat = Modify entire row - must
assign vector with the
6 7 8 9 correct length
4 5 10 7
2. Create a 6 x 6 matrix in which the middle two rows and the middle two
columns are 1s, and the rest are 0s.
24
Extending Matrix
The size of added row/column must fit the existing matrix
>> vec=[2:4;6:8];
>> vec(:,4)=[1,5]'
vec = Fourth column added
2 3 4 1
6 7 8 5
>> vec(3,:)=[2:2:8]
vec =
2 3 4 1 Third row added
6 7 8 5
2 4 6 8
>> vec(:,5)=[1 5 7] ?
25
Extending Matrix - cont’d
>> vec(:,6)=[6 7 1]’
vec =
2 3 4 1 0 6 Column padded
with 0’s
6 7 8 5 0 7
2 4 6 8 0 1
>> vec(4,3)=9
vec =
2 3 4 1 0 6
6 7 8 5 0 7
2 4 6 8 0 1
0 0 9 0 0 0
>>vec(6,7)=8 ?
Matrices can also be concatenated like vectors provided
the row/column fit each other. 26
Deleting Elements
An element, or range of elements from the existing
variables can be deleted.
Vector
>> st=2:9
st =
2 3 4 5 6 7 8 9
>> st(4)=[]
st =
2 3 4 6 7 8 9 vector
becomes
shorter
>> st(2:3)=[]
st =
2 6 7 8 9
27
Deleting Elements - cont’d
Matrix
Individual elements cannot be deleted from a matrix, but
entire row/column can be deleted.
>> mat(:,2:4)=[]
All rows column 2
mat = through 4 deleted
4 8
8 9
3 3
Delete second row ? 28
Try This !
What would be produced by the following sequence of statements and
expressions.
a) A=[N(1,1:4)’,N(2,2:5)’]
b) B=[N(:,3)' N(3,:)]
c) C(3:4,5:6)=N(2:3,4:5)
29
Array Functions
Functions used for manipulating arrays
30
Built-in Functions For Array
Function Description Example
mean(A) Mean value of elements >> A=[ 5 3 8 6];
of the vector >> mean(A)
ans =
5.5000
max(A) The largest element of >> max(A)
the vector ans =
min(A) 8
for
smallest [m,n]=max(A) m is the largest element >> [m,n]=max(A)
in vector, and n is the m =
position of the element 8
n =
3
sum(A) Sum of the elements of >> sum(A)
the vector ans =
22
sort(A) Arranges the elements in >> sort(A)
ascending order ans =
3 5 6 8 31
Built-in Functions For Array
Function Description Example
median(A) Median value of >> median(A)
elements of the vector ans =
5.5000
std(A) Standard deviation of >> std(A)
the element of the ans =
vector 2.0817
cumsum(A) / Stores cumulative >> cumsum(a)
cumprod (A) sum/product for each ans =
step as it adds the 5 8 16 22
elements from the
vector >> cumprod(a)
ans =
5 15 120 720
33
Example
>> mt= [2 5 7 4;6 9 3 5;8 7 3 1]
>> mean(mt)
ans =
5.3333 7.0000 4.3333 3.3333
>> cumsum(mt)
ans =
2 5 7 4
8 14 10 9
16 21 13 10
34
Try This !
How to get the overall maximum of a matrix.
>> mt= [2 5 7 4;6 9 3 5;8 7 3 1]
>> max(max(mt))
ans= ?
Using the ones and zeros commands, create a 4 x 3 matrix in which the first
two rows are 0s and the next two rows are 1s.
>> a=zeros(2,3);
>> a(3:4,:)=ones(2,3)
a =
0 0 0
0 0 0
1 1 1
1 1 1
35
or >> a=[zeros(2,3);ones(2,3)]
Array Functions
Functions used for managing & handling arrays
36
Array Functions - Matrix Dimension
Vector v Matrix m
4 6 5 8 3 9 8
2 5 3
42
Strings as Variables
• String is an train of characters created by typing
characters within single quotes (‘).
• Can include letters, digits, other characters and
spaces.
• String are used in output commands, formatting
commands of plot, input argument of some
functions.
• When a variable is defined as a string, the characters
are stored in array just as number are, including
spaces.
43
vector
Strings as Variables - cont’d
>> k='SKAM 1422' Create string
k = variable >> a=352
SKAM 1422 a =
Numbers are
352 right justify
>> k(4)
ans = >>s=k(5)? >> b='352'
>>length(s)? b =
M Strings are
352 left justify
>> k(4)=‘A’
Modifying a≠ b
k = a is a number
string variable
SKAA 1422 b is a string
Thank You
46