Lecture2 MatrixOperations
Lecture2 MatrixOperations
Matr ces
A matrix is a two-dimensional array of data values.
A matrix that contains n rows and m columns has a total of nxm values.
Example: D matrix contains 4x3 = 12 values.
-1 30 6
15 2 -7
D matrix -9 78 20
4 0 19
4x3
2
Def n ng Matr ces
Each row of a matrix can be defined on a separate line.
Data tems w th n a row can be separated by commas ( , ) or spaces.
B = [-1, 0, 1 ;
1, 2, 1 ;
3, 1, 2 ;
4, 0, 4 ];
>> B = [4 7 1 5 ;
2 0 3 4 ;
7 1 8 0];
>> C = A * B
Same results are
C = obtained with
36 35 29 57 manual calcula ons.
95 51 90 46
6
Examples : Matr x Operat ons
* Operator s used for normal matr x mult pl cat on.
(Us ng the l near algebra rules).
.* Operator s used for element-by-element mult pl cat on.
(Not us ng the l near algebra rules).
A = [5 6 4];
B = [ 3 A 2 ]; [3 5 6 4 2 ]
x = [1 2 3];
y = [7 9 4];
z = [x y]; [1 2 3 7 9 4]
u = [ x ; y] 1 2 3
7 9 4
>> A = [1 2 3 ; 4 5 6 ; 7 8 9]
A = 1 2 3
4 5 6
7 8 9
13
14
end keyword
>> A = [1 2 3 ; 4 5 6 ; 7 8 9]
A = 1 2 3
4 5 6
The end keyword 7 8 9
can be used as the Gets the
element n last
last index in an >> A(2, end) column of row 2
indexing expression. ans =
6
Gets the element
n last row of
>> A(end, 2) column 2
ans =
8
Gets the element n
>> A(end) last column and last row
ans =
9
Gets the elements
n last two columns
>> A(3, 2:end) of row 3
ans =
8 9
15
Vectorization Examples:
a = 1 : 5
a = [ 1 : 5 ]
16
General Form of Vector zat on
General form of vector zat on:
Var ableName = StartValue : IncrementValue : EndValue
>> a = 1 : 5
a =
1 2 3 4 5
>> a = 1 : 2 : 5
a =
1 3 5
>> a = 1 : 2 : 8
a =
1 3 5 7
17
t =
0 0.5 1.0 1.5 2.0 2.5
A =
10 9 8 7 6 5 4 3 2
ans =
Columns 1 through 8
1.0000 1.4444 1.8889 2.3333
2.7778 3.2222 3.6667 4.1111
Columns 9 through 10
4.5556 5.0000
19
>> a = []
a =
[]
20
Spec al Matr ces
Bu lt- n
Explanat on
Funct on name
zeros(n) Returns a n × n matrix of zeros
21
22
Array Operations
Element-by-element array operations can be performed one at a t me.
>> A = 10 : 10 : 50
A =
10 20 30 40 50
>> B = 20 : 10 : 60
B =
20 30 40 50 60
>> C C =
C = 200 600 1200 2000 3000
200 600 1200 2000 3000
23
Matlab
Explanat on Form Example Result vector
Operator
Scalar add t on to
+ A+ s [4 , 6] + 3 [7 , 9]
array
Scalar subtract on
- A- s [4 , 6] - 3 [1 , 3]
from array
Scalar
* mult pl cat on of A* s [4 , 6] * 3 [12 , 18]
array
Scalar d v s on of
/ A/ s [4 , 6] / 2 [2 , 3]
array
Scalar
.^ exponent at on of A .^ s [2 , 5] .^ 3 [8 , 125]
array
24
Element-by-element Operat ons
(A and B are both vectors)
Matlab
Explanat on Form Example Result vector
Operator
Vectors
+ A+ B [4 , 6] + [8 , 1] [12 , 7]
add t on
Vectors
- A-B [4 , 6] – [8 , 1] [-4 , 5]
subtract on
Vectors
.* mult pl cat o A .* B [4 , 6] .* [8 , 1] [32 , 6]
n
Vectors r ght
d v s on
./ A ./ B [4 , 6] ./ [8 , 1] [0.5 , 6.0]
(normal
d v s on)
Vectors left
.\ A .\ B [4 , 6] .\ [8 , 1] [2.0 , 0.167]
d v s on
Vectors
.^ exponent at A .^ B [2 , 5] .^ [6 , 4] [64 , 625]
on
25
Example Problem
Convert a l st of Degrees to Rad ans.
Both * and .* operators can be used.
For convers on, Degrees vector s mult pl ed by p , wh ch s a scalar value.
Radians =
0.1745 0.2618 1.2217 1.5708
Radians = Same
0.1745 0.2618 1.2217 1.5708 results
26
Example1: Matr x exponent (^) operator
>> a = [1 2 ; 3 4]
a =
1 2
3 4
>> a ^ 3
>> a * a * a 1 2 1 2 1 2
* *
3 4 3 4 3 4
ans =
37 54
81 118
27
Example2: Element-by-element
exponent (.^) operator
>> a = [1 2 ; 3 4]
a =
1 2
3 4
>> a .^ 3
a .^ 3 s means element-by-element power
ans = of each value n the matr x a.
1 8
27 64 13 23
33 43
28
Mult pl cat on of a Matr x
w th a Scalar Value
A = [ 1 : 4 ; -1 : -1 : -4 ; 3 1 2 -1 ]
B = A .* 5
A= B=
29
Tablo =
10 5
20 10
30 15
40 20
>> Tablo'
ans =
10 20 30 40
5 10 15 20 30
Bas c Input/Output
31
32
User Input from Keyboard
If more than one value is to be entered by user, user must separate values with
space or commas, and enclose all data with left and right brackets [ ].
A =
2 -5 6 4 7
If you are nputt ng a str ng, then you should wr te the 'S' letter
as spec f er n nput command.
>> M = [1 2 ; 3 4];
>> disp(M)
1 2
3 4
34
Examples of d sp funct on
The d sp funct on can d splay more than one var able.
Us ng brackets [ ] allows only same type of var ables.
If str ngs and numbers are wr tten n same l st, only the str ngs are d splayed.
Us ng curly parantheses { } allows each tem as a separate vector.
Formatted pr nt ng funct on
fprintf() command can be used for formatted printing.
fprintf (format_string, variables)
36
Us ng fpr ntf for scalar values
37
PuanTablosu =
1 2 3 4 F rst row represents Müsabaka numbers
75 88 102 93
Second row represents scores of Takım1
Th rd row represents scores of Takım2
99 84 95 105
You can use the format command n command w ndow to change the
d splay format of float and double numbers (fract onal).
Default format s short.
39