Professional Documents
Culture Documents
Tutorial 02 (Matrices)
Tutorial 02 (Matrices)
>> a = 1
format compact % gia mikrotero xwro
>> a = 1
% oi entoles pou eisagw exoun mprosta tous to '>>', to apotelesma (apo katw) den
exei tipota
% sel 33 Gilat
% ti einai ena dianusma sto xwro? (sxhma)
% ti einai pinakas? (sxhma)
% h eisagwgh pinakwn kai dianusmatwn ginetai me xrhsh agkulwn [], OXI paren8esewn
()
>> a = (1,2,3)
Error
>> a = [-3,4,0]
a =
-3 4 0
>> a = [-3 4 0]
a =
-3 4 0
>> ['H', 'e', 'y'] % mia akolou8ia xarakthrwn einai pinakas pou periexei
grammata / 8a ta doume pio meta
ans =
'Hey'
>> b = [
1
2
-4
3
]
b =
1
2
-4
3
>> b = [1 ; 2 ; -4 ; 3]
b =
1
2
-4
3
% mporei mesa sto dianusma na exoume sunarthseis h mathimatikes ekfraseis, oxi mono
arithmous
>> A = [ 1 2 3 4
5 6 7 8
9 1 2 3
4 5 6 7]
A =
1 2 3 4
5 6 7 8
9 1 2 3
4 5 6 7
>> A = [1 2 3 4 ; 5 6 7 8 ; 9 1 2 3 ; 4 5 6 7]
A =
1 2 3 4
5 6 7 8
9 1 2 3
4 5 6 7
>> whos
Name Size Bytes Class Attributes
% prakseis pinakwn
>> A1 = [1 2 ; 3 4]; % me to ';' sto telos, den emfanizetai h eksodos ths
entolhs
>> A2 = [2 4 ; 7 9];
>> A1
A1 =
1 2
3 4
>> A2
A2 =
2 4
7 9
% sthn pros8esh kai afairesh pinakwn, prepei oi duo pinakes na exoun idies
diastaseis
% prosoxh ston pollaplasiamo pinakwn: oi diastaseis prepei na sumfwnoun
% an o A exei diastaseis mxn kai o B exei nxk, tote o A*B exei mxk
>> [1 2 ; 4 5] ^ 2 % idio me [1 2 ; 4 5] * [1 2 ; 4 5]
ans =
9 12
24 33
% gia thn upswsh pinaka se dunamh, prepei o pinakas na einai nxn (tetragwnikos)
% isxuei A^2 = A*A, kai genika A^n = A*A*...*A (n fores)
>> [1 2 3] * [1 2 3]'
ans =
14
>> [1 2 3]' * [1 2 3]
ans =
1 2 3
2 4 6
3 6 9
>> whos
Name Size Bytes Class Attributes
>> det(A)
..
% EIDIKOI PINAKES
>> zeros(4,5) % pinakas me 0
ans =
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
>> eye(4,4) % tautotikos pinakas (ti idiothta exei? / poia h sxesh tou me ton
tautotiko pinaka?)
ans =
1.00 0 0 0
0 1.00 0 0
0 0 1.00 0
0 0 0 1.00
>> format short g % to format DEN allazei ton upologismo twn metavlhtwn, mono thn
emfanish tous
>> eye(4,4)
ans =
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
>> magic(4) % n = 4
ans =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
% h entolh rand(m,n) dinei enan mxn pinaka tuxaiwn stoixeiwn apo omoiomorfh
katanomh sto [0,1]
>> rand() % xwris parametro, dinei enan tyxaio ari8mo sto [0,1]
ans =
0.09754
% an 8elw tuxaio ari8mo apo omoiomorfh katanomh px sto [10,11], grafw 10+rand()
% more on kai more off -> gia na deixnei tis plirofories ligo ligo sthn ektupwsh, q
gia eksodo
>> more on
% help rand kai help elmat (katalogos eidikwn pinakwn - elementary matrices)
% help gallery (sunarthseis pinakwn) kai help private/(onoma sunarthshs apo
gallery)
>> u = [1 : 2 : 9]
u =
1 3 5 7 9
>> A = [1 2 3 4 5 ; 10 8 6 4 2]
A =
1 2 3 4 5
10 8 6 4 2
>> B = [1 2 3 4 5 ; 2 4 6 8 10 ; 3 6 9 12 15]
B =
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
% o B ?
>> u = [0 : 0.1 : 1]
u =
Columns 1 through 7
0 0.1 0.2 0.3 0.4 0.5
0.6
Columns 8 through 11
0.7 0.8 0.9 1
% allagh stoixeiou
>>u(1) = 5
..
>> u
..
% diagrafh stoixeiou
>> u(1) = []
..
>> u(3:6) = []
..
>> u
..
% pros8hkh stoixeiwn
>> w = [1 2]
w =
1 2
>> w(5) = 1 % stis endiameses 8eseis pou den phran timh, ekxwreitai to 0
w =
1 2 0 0 1
>> w(7:10) = 3
w =
1 2 0 0 1 0 3 3 3 3
>> clear w
>> u
u =
Columns 1 through 7
0 0.1 0.2 0.3 0.4 0.5
0.6
Columns 8 through 11
0.7 0.8 0.9 1
>> v = u(2:10)
v =
Columns 1 through 7
0.1 0.2 0.3 0.4 0.5 0.6
0.7
Columns 8 through 9
0.8 0.9
>> v = u(2:2:10)
v =
0.1 0.3 0.5 0.7 0.9
>> u(10 : -6 : 4)
ans =
0.9 0.3
>> u
u =
Columns 1 through 7
0 0.1 0.2 0.3 0.4 0.5
0.6
Columns 8 through 11
0.7 0.8 0.9 1
>> u(1:22)
Error...
>> u(1:1)
ans =
0
>> u(2:2)
ans =
0.1
>> whos
Name Size Bytes Class Attributes
A 2x5 80 double
A1 2x2 32 double
A2 2x2 32 double
B 3x5 120 double
C 3x3 72 double
a 1x1 8 double
ans 1x1 8 double
b 3x1 24 double
u 1x11 88 double
v 1x0 0 double
>> A
A =
1 2 3 4 5
10 8 6 4 2
% h entolh A(i,j) dinei to stoixeio tou pinaka A sthn i grammh kai j sthlh
>> A(1,1)
ans =
1
>> A(2,1)
ans =
10
A
% A(:,j) = dianusma = h j sthlh tou pinaka A
% A(i,:) = dianusma = h i grammh tou pinaka A
% A(m:n , p:s) = o upopinaka tou A me grammes apo m ws n kai sthles apo p ws s
% A(end,:) = dianusma = h teleutaia grammh tou pinaka A
% A(:,end) = dianusma = h teleutaia sthlh tou pinaka A
>> A(:,2)
ans =
2
8
>> A(3:)
Error
>> A(1:)
Error
>> whos
Name Size Bytes Class Attributes
A 2x5 80 double
A1 2x2 32 double
A2 2x2 32 double
B 3x5 120 double
C 3x3 72 double
a 1x1 8 double
ans 2x3 48 double
b 3x1 24 double
u 1x11 88 double
v 1x0 0 double
>> B
B =
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
>> A = [1 2 3 4 ; 5 6 7 8 ; 9 10 11 12 ; 13 14 15 16]
A =
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
% ftiaxnw ena pinaka me IDIA diastash me auton pou 8elw, me oti stoixeia 8elw
>> u = [1:16]
u =
Columns 1 through 6
1 2 3 4 5 6
Columns 7 through 12
7 8 9 10 11 12
Columns 13 through 16
13 14 15 16
>> u(:)
ans =
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
>> A(:)
ans =
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
>> A(:) = u
A =
1 5 9 13
2 6 10 14
3 7 11 15
4 8 12 16
clc
% pws 'megalwnw' ena pinaka?
>> A
A =
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
>> v = [1:4]'
v =
1
2
3
4
% kollaw to dianusma sthlhs v deksia apo ton pinaka A (to anapodo?) / prosoxh stis
diastaseis tous
>> u
u =
Columns 1 through 6
1 2 3 4 5 6
Columns 7 through 12
7 8 9 10 11 12
Columns 13 through 16
13 14 15 16
% sunepws mporw se ena dianusma na pros8esw oti stoixeia 8elw, se opoiadhpote 8esh
8elw
>> A
A =
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
>> v
v =
1
2
3
4
% edw pros8etw to dianusma grammhs v sthn teleutaia grammh tou pinaka A (oi
diastaseis prepei na sumfwnoun)
>> E = [A B]
E =
1 2 -4 1
3 4 2 0
>> F = [A ; B]
F =
1 2
3 4
-4 1
2 0
>> G = [A B ; I O]
G =
1 2 -4 1
3 4 2 0
1 0 0 0
0 1 0 0
% An 8elw na lusw ena grammiko susthma me morfh A*x=b (opws prin) h na kanw
apaloifh Gauss, xrhsimopoiw ton
% pinaka [A b], o opoios kaleitai epaukshmenos pinakas
>> G
G =
1 2 -4 1
3 4 2 0
1 0 0 0
0 1 0 0
>> det(G)
ans =
-2
>> G(1,3) = 4
G =
1 2 4 1
3 4 2 0
1 0 0 0
0 1 0 0
% H SYNARTHSH CAT
% auth h sunarthsh sunenwnei 2 h pio pollous pinakes kata mhkos mias diastashs
% h cat(1,A,B) sunenwnei tous A kai B sthn die8unsh ston y (panw katw)
% exei to idio apotelesma me thn [A ; B]
% h cat(2,A,B) dhmiourgei ton [A B]
% h cat(3,A,B) dhmiourgei ena pinaka nxnx2 ws pros ton aksona z (vazei ton ena
pinaka mprosta
% apo ton allon)
>> A = zeros(3)
A =
0 0 0
0 0 0
0 0 0
>> A(:)
..
>> u = [1:9]
u =
Columns 1 through 6
1 2 3 4 5 6
Columns 7 through 9
7 8 9
>> A(:) = u
A =
1 4 7
2 5 8
3 6 9
>> A = A'
A =
1 2 3
4 5 6
7 8 9
>> B = zeros(3)
B =
0 0 0
0 0 0
0 0 0
>> u = [1:2:17];
>> cat(3,A,B)
ans(:,:,1) =
1 2 3
4 5 6
7 8 9
ans(:,:,2) =
1 3 5
7 9 11
13 15 17
>> whos
Name Size Bytes Class Attributes
A 3x3 72 double
B 3x3 72 double
ans 3x3x2 144 double
u 1x9 72 double
>> sin(pi/2)
ans =
>> sin(pi/4)
ans =
0.7071
>> sin_ = sin(u) % de dinw onoma metavlhths idio me kapoia desmeumenh leksh (opws
to sin)
>> sin_
..
clc
>> sin(u)
ans =
0 0.20 0.39 0.56 0.72
0.84
>> exp(u)
ans =
1.00 1.22 1.49 1.82 2.23
2.72
>> u = [1:2:10]
u =
1 3 5 7 9
>> v = [2:3:14]
v =
2 5 8 11 14
2 15 40 77 126
>> v .\ u
ans =
>> A = [3 5 7 ; 3 4 5 ; 6 7 8]
A =
3 5 7
3 4 5
6 7 8
>> B = [1 2 3 ; 5 6 7 ; 3 4 5]
B =
1 2 3
5 6 7
3 4 5
3 10 21
15 24 35
18 28 40
49 64 79
38 50 62
65 86 107
>> 3 + A
ans =
6 8 10
6 7 8
9 10 11
>> A + 3
ans =
6 8 10
6 7 8
9 10 11
>> A - 3
ans =
0 2 4
0 1 2
3 4 5
>> 3 - A
ans =
0 -2 -4
0 -1 -2
-3 -4 -5
>> u = [1:6]
u =
1 2 3 4 5 6
>> a = u .^ 2 % idio me to u .* u
a =
1 4 9 16 25 36
>> a = sqrt(a)
a =
1 2 3 4 5 6
% edw vriskw to dianusma opou ta stoixeia tou exoun typo (x^3 + 5*x)/(4*x^2 - 10),
gia ka8e arithmo x tou a
% max,min,length,sort,sum,prod,norm,median,mean,std
% megisto stoixeio dianusmatos,megisto stoixeio dianusmatos,mhkos dinusmatos,
% auksousa taksinomhsh stoixeiwn dianusmatos, a8roisma stoixeiwn, ginomeno
stoixeiwn
% norma, diamesos, mesh timh, tupikh apoklish
% help sort
>> u = [-1 4 2 6 -3 -10 0 3 2]
>> u = [-1 4 2 6 -3 -10 0 3 2]
u =
-1 4 2 6 -3 -10 0 3 2
% na vre8ei to elaxisto, megisto, elaxisto kata apoluth timh, megisto kata apoluth
timh
% stoixeio tou u
% telos, na vroume posous arithmous periexei, na taksinomhsoume me f8inousa seira
% kai na vroume to a8roisma kai to ginomeno twn stoixeiwn tou.
>> u = [-2:0.2:2]
u =
Columns 1 through 9
Columns 10 through 18
Columns 19 through 21
>> length(u)
ans =
21
>> v = exp(u)
v =
Columns 1 through 9
Columns 10 through 18
0.8187 1.0000 1.2214 1.4918 1.8221 2.2255 2.7183 3.3201
4.0552
Columns 19 through 21
>> plot(u,v)
-10 -8 -6 -4 -2 0 2 4 6 8 10
>> v = exp(u)
v =
Columns 1 through 9
Columns 10 through 11
0.2981 2.2026
>> plot(u,v)
% ti parathrw?
>> u = [-10:2:10];
>> v = u.^2
v =
100 64 36 16 4 0 4 16 36 64 100
% H ENTOLH LINSPACE
>> linspace(0.1,1,10)
ans =
Columns 1 through 7
Columns 8 through 10
0.8 0.9 1
Columns 1 through 7
Columns 8 through 11
% 8a xsanaftiaksw thn grafikh ths y=x^2 sto [-10,10] me polla perissotera shmeia
sto
% pedio orismou ths
>> u = linspace(-10,10,1000);
>> v = u.^2;
>> plot(u,v)
19
-8 1 3
% max,min,diag,size,reshape,length,norm,det,trace,rank,inv,eig,poly
>> help matfun
>> A = ones(4,3)
A =
1 1 1
1 1 1
1 1 1
1 1 1
ans =
ans =
4 3
% allazoun oi diastaseis tou pinaka. o neos pinakas prepei na exei osa stoixeia
exei o palios
>> A = reshape(A, 2, 6)
..
>> C = zeros(4,5)
C =
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
80 82 69 63 9
95 89 36 3 92
65 64 59 24 57
4 71 16 97 26
95 89 69 97 92
82 95 65 97
>> max(max(C))
ans =
97
>> min(min(C))
ans =
>> A = [1 -3 3 ; 3 -5 3 ; 6 -6 4]
A =
1 -3 3
3 -5 3
6 -6 4
>> size(A)
ans =
3 3
>> det(A)
ans =
16
% afou o pinakas A einai tetragwniko kai exei mh mhdenikh orizousa, shmainei oti
orizetai o antistrofos tou
>> inv(A)
ans =
>> A
A =
1 -3 3
3 -5 3
6 -6 4
>> rank(A)
ans =
>> eig(A)
ans =
4 + 0i
-2 + 1.1047e-15i
-2 - 1.1047e-15i
>> A = [2 0 -2 ; 0 4 0 ; -2 0 5]
A =
2 0 -2
0 4 0
-2 0 5
>> eig(A)
ans =
1
4
6
-0.89443 0 -0.44721
0 1 0
-0.44721 0 0.89443
D =
1 0 0
0 4 0
0 0 6
1 -11 34 -24