Professional Documents
Culture Documents
T2 Vector and Matrix
T2 Vector and Matrix
T2 Vector and Matrix
TOPIC 2
1
'
Vectors
Creating Row & Column vectors
Referring to elements
Changing, Extending & Deleting elements
>> load=[3 7 5 4]
load =
3 7 5 4
>> dist=[1,3,4,6]
dist =
1 3 4 6
2
'
>> load=1:3:15
Without [ ]
load =
1 4 7 10 13
3
'
>> rowv=3:4:11;
>> colv=rowv'
colv =
3
7
11 No direct way using colon
operator to get column vector
8
4
'
4 7 6 0 2
10
5
'
>> st(4)=[]
st =
2 3 4 6 7 8 9
Practice Problem
Do not type the vectors explicitly
6
'
Matrices
Creating
Modifying
Special Matrices
13
7
'
15
Special Matrices
zeros >> z0=zeros(2,3)
z0 = >>zeros(3)?
0 0 0
0 0 0
8
'
Practice Problem
17
>> k1=k(1:2,2:3)
k1 = Extracting a subset of a matrix:
3 4 Row 1‐2, col 2‐3
5 7 18
9
'
>> k(:,5)
ans = Extracting entire column 5
6 k2=k(2,3:end)?
11
k3=k(end,3)?19
Modifying Variables
20
10
'
11
'
Extending 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] ?
23
>> 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. 24
12
'
>> mat(:,2:4)=[]
All rows column 2
mat =
through 4 deleted
4 8
8 9
3 3
Delete second row ? 25
Practice Problem
Create vector v = 7 5 3 1. Use v to create the following
matrices;
a) m1 = 7 5 3 1 7 5 3 1
b) m2 = 7 5 3 1
7 5 3 1
c) m3 = 7 7
5 5
3 3
1 1
13
'
Array Functions
Functions used for manipulating arrays
27
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
28
14
'
15
'
31
32
16
'
Example
>> cumsum(mt)
ans =
2 5 7 4
8 14 10 9
16 21 13 10
33
Try This !
34
17
'
Array Functions
Functions used for managing & handling arrays
35
18
'
19
'
Try This !
What would be produced by the following sequence of statements
and expressions.
– mt=[7:-2:3;23 6 9;3:5]
– mt(2,3)
– mt(1,:)
– mt(:,4)=[7 3 2]‘
– size(mt)
– length(mt)
– numel(mt)
– vc=mt(2,:)
– vc(vc(4))
– vc(1:2)=[]
– reshape(mt,4,3)
– zeros(size(mt))
40
20
'
Strings as Variables
41
Strings as Variables
• String is a 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.
42
21
'
vector
Strings as Variables
>> k='SKAM 1422' Create string >> a=352
variable
k = a =
SKAM 1422 352 Numbers are
right justify
>> k(4)
>>s=k(5)? >> b='352'
ans =
>>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
matrix
Strings as Variables
• String can also be placed in matrix; using semicolon or enter
key, but all rows must have the same length, which may
cause problem when creating rows of specific wording.
• Matlab has char function that creates rows with the same
number of characters by automatically adding spaces to the
shorter rows.
• Coma is used to separate the rows.
22
'
Practice Problem
1. Write the shortest Matlab statement for the following problems.
a) sum of even numbers from 2 to 100
b) Create the following matrix by typing one command. Do not type
individual elements explicitly.
1 2 3 4 1 0 7 0 0 0 0
k = 1 1 1 1 h = 2 0 6 m = 0 0 7 7
5 5 5 5 3 0 5 0 0 7 7
Practice Problem
2. Create three row vectors:
a = [3 6 ‐4 7 2] b = [6 ‐8 5 9 3] c = [5 ‐2 7 0 8]
Use the three vectors to create the following matrices.
(a) the first, second, and third rows consist of the first three elements
of the vectors a, b, and c, respectively.
(b) the first, second, and third columns consist of the last three
elements of the vectors a, b, and c, respectively.
3. Using the zeros, ones, and eye commands create the following arrays
1 0 0 1 1 0 0 1 1
0 1 0 1 1 0 0 1 1
0 0 0 0
1 1 1 1
23
'
Practice Problem
4. Create matrix A. 1 2 3 4 5 6
A= 7 8 9 10 11 12
13 14 15 16 17 18
Practice Problem
5. Create matrix k.
4 8
k= 16 2
4 8 ‐4 ‐8
a) Use k to create a 4x4 matrix m. 16 2 ‐16 ‐2
m= ‐4 ‐8 4 8
‐16 ‐2 16 2
4 1 ‐4 ‐8
b) Modify matrix m be as shown. 1 1 1 1
m= ‐4 1 4 8
‐16 1 16 2
24
'
Thank You
50
25