Download presentation
Presentation is loading. Please wait.
1
第 2 章 陣列(Array)與矩陣(Matrix)的運算
2019/4/26 Puff! The magic dragon, live by the sea…
2
2019/4/26 Puff! The magic dragon, live by the sea…
3
列向量 & 行向量 Row vector 列向量 [5, 7, 2]; [3, 4, -6, 7] Column vector 行向量
2019/4/26 Puff! The magic dragon, live by the sea…
4
陣 列 Arrays 2019/4/26 Puff! The magic dragon, live by the sea…
5
轉置 transpose 2019/4/26 Puff! The magic dragon, live by the sea…
6
Transpose matrix g=[1;2;3;4]; g1=g’; or g=[1 2 3 4];
2019/4/26 Puff! The magic dragon, live by the sea…
7
Transpose matrix k=[1 -9 1 0 3; -4 4 1 1 2; 24 2 2 36 2] or
k1=k’ 2019/4/26 Puff! The magic dragon, live by the sea…
8
產生等間距大向量 x=[m:q:n] ?x=[0:2:8] x = 0 2 4 6 8 ?y=[-3:2] y =
?y=[-3:2] y = 2019/4/26 Puff! The magic dragon, live by the sea…
9
產生等間距大向量 ?z=[0:0.1:2] z = Columns 1 through 7
Columns 8 through 14 Columns 15 through 21 2019/4/26 Puff! The magic dragon, live by the sea…
10
產生等間距大向量 ?zzz=[3:-0.5:0] zzz =
2019/4/26 Puff! The magic dragon, live by the sea…
11
linespace(x1,x2,n) 產生等間距列向量
the same as [5:0.1:8] n 表示列向量內含的元素個數 如果省略則內定值為1 2019/4/26 Puff! The magic dragon, live by the sea…
12
logspace(x1,x2,n) 產生具有對數等間距的向量
logspace (a, b, n) 在10a~10b間(含)取的點數 logspace(-1,1,4)=[0.1, ,2.1544, 10.0] 2019/4/26 Puff! The magic dragon, live by the sea…
13
2-Dimensional Arrays Puff! The magic dragon, live by the sea…
2019/4/26 Puff! The magic dragon, live by the sea…
14
合併 矩陣(行數增加) 矩陣A與r需具有相同的列數 ?A=[2,4,10; 16,3,7] A = 2 4 10 16 3 7
?r=[1,1;0,0] r = ?B=[A r] B = 矩陣A與r需具有相同的列數 2019/4/26 Puff! The magic dragon, live by the sea…
15
合併 矩陣(列數增加) 矩陣A與r需具有相同的行數 ?A=[2,4,10; 16,3,7] A = 2 4 10 16 3 7
?r=[1,2,3] r = ?B=[A; r] B = 矩陣A與r需具有相同的行數 2019/4/26 Puff! The magic dragon, live by the sea…
16
Transpose matrix (complex conjugate)
?A=[3+2*i, 2+6*i; 5+4*i, 3+5*i] A = i i i i ?A' ans = i i i i 2019/4/26 Puff! The magic dragon, live by the sea…
17
Array addressing定址 修改矩陣內元素 D(1,3)=6 B = 2 4 10 16 3 7 1 2 3 ?B(2,2)=0
?B(2,2)=0 2019/4/26 Puff! The magic dragon, live by the sea…
18
Array addressing定址 2. V(:) V向量內所有元素 ?V=[1 2 3 4 5] V = 1 2 3 4 5 ?V(:)
?V(:) ans = 1 2 3 4 5 2019/4/26 Puff! The magic dragon, live by the sea…
19
Array addressing定址 3. V(2:4) V(2), V(3), V(4), V(5), V(6) V =
?V(2:4) ans = 2019/4/26 Puff! The magic dragon, live by the sea…
20
Array addressing定址 A(:,3) 矩陣A第三行所有的元素 A = 3 5 11 17 1 8 2 3 4 ?A(:,3)
?A(:,3) ans = 11 8 4 2019/4/26 Puff! The magic dragon, live by the sea…
21
Array addressing定址 5. A(:,2:3) 矩陣A第2行至第3行所有的元素 A = 3 5 11 17 1 8 2 3 4
?A(:,2:3) ans = 2019/4/26 Puff! The magic dragon, live by the sea…
22
Array addressing定址 6. A(2:3,1:3)矩陣A第2至第3列,第1至第3行所有的元素 (可用以分割矩陣) A =
?A(2:3,2:3) ans = 2019/4/26 Puff! The magic dragon, live by the sea…
23
?B=A(:,5:-1:1) %把A陣列行排列的次序顛倒過來 B = 3 0 4 9 6 0 0 7 5 1
?A(1,5)=3 ?B=A(:,5:-1:1) %把A陣列行排列的次序顛倒過來 B = 2019/4/26 Puff! The magic dragon, live by the sea…
24
B = C = ?B(2,:)=C 2019/4/26 Puff! The magic dragon, live by the sea…
25
?E=D([2,2,2,1],:) %將陣列D的第二列重複三次,第一列重複一次 E =
?E=D([2,2,2,1],:) %將陣列D的第二列重複三次,第一列重複一次 E = 2019/4/26 Puff! The magic dragon, live by the sea…
26
Using ‘clear’ to avoid errors
clear A 2019/4/26 Puff! The magic dragon, live by the sea…
27
A = 6 9 4 0 3 1 5 7 0 0 ?max(A) ans = 6 9 7 0 3 ?[x,k]=max(A) x =
?max(A) ans = ?[x,k]=max(A) x = k = % index 2019/4/26 Puff! The magic dragon, live by the sea…
28
B = 6 9 4 0 3 ?max(B) ans = 9 Puff! The magic dragon, live by the sea…
?max(B) ans = 9 2019/4/26 Puff! The magic dragon, live by the sea…
29
?length(A) %傳回m*n (3*2)中的最大值 3
?max(A) ans = ?min(A) ?size(A) ?length(A) %傳回m*n (3*2)中的最大值 3 2019/4/26 Puff! The magic dragon, live by the sea…
30
?A=[6 2; ;3+4*i 0] A = i ?max(A) ans = ?min(A) 2019/4/26 Puff! The magic dragon, live by the sea…
31
A = i ?sum(A) %將同一行中的所有元素加總 ans = i 2019/4/26 Puff! The magic dragon, live by the sea…
32
?sort(A) %依同行中元素大小順序排列 ans =
i ?sort(A) %依同行中元素大小順序排列 ans = 2019/4/26 Puff! The magic dragon, live by the sea…
33
2019/4/26 Puff! The magic dragon, live by the sea…
34
2019/4/26 Puff! The magic dragon, live by the sea…
35
2019/4/26 Puff! The magic dragon, live by the sea…
36
Magnitude, length, and absolute value of a Vector
Self testing p.79 2019/4/26 Puff! The magic dragon, live by the sea…
37
多維陣列multidimensional arrays
m*n*q m:列(row), n:行(column), q:頁(page) 第n頁的所有元素 A(:,:,n) 2019/4/26 Puff! The magic dragon, live by the sea…
38
?A=[4,6,1;5,8,0;3,9,2] A = ?A(:,:,2)=[6,2,9;0,3,1;4,7,5] A(:,:,1) = A(:,:,2) = 2019/4/26 Puff! The magic dragon, live by the sea…
39
?A=[8 2; 9 5] A = 8 2 9 5 ?B=[4 6; 7 3] B = 4 6 7 3 ?C=cat(3,A,B)
?B=[4 6; 7 3] B = ?C=cat(3,A,B) C(:,:,1) = C(:,:,2) = 2019/4/26 Puff! The magic dragon, live by the sea…
40
cat(1,A,B)=[A;B] 一維 A = ?[A;B] 8 2 ans = 9 5 8 2 B = 9 5 4 6 4 6 7 3
B = ?cat(1,A,B) ans = ?[A;B] ans = 2019/4/26 Puff! The magic dragon, live by the sea…
41
cat(2,A,B)=[A,B] 二維 A = ?[A,B] 8 2 ans = 9 5 B = 8 2 4 6 4 6 9 5 7 3
B = ?cat(2,A,B) ans = ?[A,B] ans = 2019/4/26 Puff! The magic dragon, live by the sea…
42
cat(3,A,B) 三維 A = ans(:,:,2) = 8 2 4 6 9 5 7 3 B = 4 6 7 3 ?C(2,1,1)
B = ?cat(3,A,B) ans(:,:,1) = ans(:,:,2) = ?C(2,1,1) ans = 9 2019/4/26 Puff! The magic dragon, live by the sea…
43
Array Operations B = 4 6 7 3 ?A+B ans = 12 8 16 8 ?A-3*B -4 -16 -12 -4
?A+B ans = ?A-3*B A = ?3*A ans = ?A+1000 ans = ?A/3 2019/4/26 Puff! The magic dragon, live by the sea…
44
Array Operations ?A./B %(B.\A) ans = 2.0000 0.3333 1.2857 1.6667
B = ?A*B ans = ?A.*B ?A./B %(B.\A) ans = ?B./A %(A.\B) ?A\B %(A-1*B) ans = ?A/B %(A*B-1) 2019/4/26 Puff! The magic dragon, live by the sea…
45
2019/4/26 Puff! The magic dragon, live by the sea…
46
2019/4/26 Puff! The magic dragon, live by the sea…
47
2019/4/26 Puff! The magic dragon, live by the sea…
48
Take a rest! Try p. 84 ex.2.3-1 Puff! The magic dragon,
2019/4/26 Puff! The magic dragon, live by the sea…
49
Inner matrix dimensions must agree.
?x'*y ans = ?x*y' 38 ?x=[2,4,-5]; y=[-7,3,-8] ?x.*y ans = ?x*y ??? Error using ==> * Inner matrix dimensions must agree. 2019/4/26 Puff! The magic dragon, live by the sea…
50
p.86, ex.2.3-2 ?v=w-r ?r=[55,36,25]; v = ?w=[-20,59,15]; -75 23 -10
?dist1=sqrt(sum(r.*r)) dist1 = 70.327 ?v=w-r v = ?dist2=sqrt(sum(v.*v)) dist2 = 2019/4/26 Puff! The magic dragon, live by the sea…
51
Ex.2.3-3 ?t=[0:.003:0.5]; ?y=exp(-8*t).*sin(9.7*t+pi/2);
?plot(t,y),xlabel('t (sec)'),... ylabel('y(t)') 2019/4/26 Puff! The magic dragon, live by the sea…
52
p.89, ex.2.3-4 ?d=[560,440,490,530,370] %mile ?t=[10.3, 8.2, 9.1, 10.1, 7.5] % hour ?speed=d./t speed = 2019/4/26 Puff! The magic dragon, live by the sea…
53
Array exponentiation ?A=[4 -5;2 3] ?C=3.^A A = C = 4 -5 81.0000 0.0041
?B=A.^3 B = ?C=3.^A C = 2019/4/26 Puff! The magic dragon, live by the sea…
54
I=V/R, P=V^2/R ?R=[10000, 20000, 35000, , ]; ?V=[120, 80, 110, 200, 350]; ?current=V./R current = ?power=V.^2./R power = 2019/4/26 Puff! The magic dragon, live by the sea…
55
?L=100*(x/0.6).^(0.625).*((1-x)/0.4).^(-1.625);
?plot(L,x), grid, xlabel('L (mol)'),... ylabel('x (mol B/mol)'), [L, x]=ginput(1) L = x = 0.5219 2019/4/26 Puff! The magic dragon, live by the sea…
56
Multivariable functions
Ex-2.3-7 v=10, 12, 14, 16, 18, 20 m/s =50, 60, 70, 80 degrees g=9.8; v=[10:2:20]; th=[50:10:80] thr=th*(pi/180); vel=[ ]; for k=1:length(th) vel=[vel, v']; end theta=[ ]; for k=1:length(v) theta=[theta; thr]; h=(vel.^2.*(sin(theta)).^2)/(2.*g) H=[v', h]; table=[0, th; H] 2019/4/26 Puff! The magic dragon, live by the sea…
57
2019/4/26 Puff! The magic dragon, live by the sea…
58
Ax=b 2019/4/26 Puff! The magic dragon, live by the sea…
59
2019/4/26 Puff! The magic dragon, live by the sea…
60
2019/4/26 Puff! The magic dragon, live by the sea…
61
2019/4/26 Puff! The magic dragon, live by the sea…
62
Matrix operation Multiplication of vectors p.95
Vector-matrix multiplication p.96 Matrix-matrix multiplication p.96 The general matrix multiplication case p.99 2019/4/26 Puff! The magic dragon, live by the sea…
63
2019/4/26 Puff! The magic dragon, live by the sea…
64
2019/4/26 Puff! The magic dragon, live by the sea…
65
2019/4/26 Puff! The magic dragon, live by the sea…
66
Special matrices Null matrix Identity matrix Puff! The magic dragon,
2019/4/26 Puff! The magic dragon, live by the sea…
67
Special matrices ?eye(3) ?ones(3) ans = ans = 1 0 0 1 1 1 0 1 0 0 0 1
?ones(3) ans = 2019/4/26 Puff! The magic dragon, live by the sea…
68
Special matrices ?zeros(3) ?zeros(2,3) ans = ans = 0 0 0 0 0 0
?zeros(2,3) ans = 2019/4/26 Puff! The magic dragon, live by the sea…
69
Special products cross(A,B) ~ momentum dot(r,F)
2019/4/26 Puff! The magic dragon, live by the sea…
70
2019/4/26 Puff! The magic dragon, live by the sea…
71
2019/4/26 Puff! The magic dragon, live by the sea…
72
polyminals [a1, a2,… an-1, an, an+1] Puff! The magic dragon,
2019/4/26 Puff! The magic dragon, live by the sea…
73
Polynomial algebra f=[9, -5, 3, 7] g=[6, -1, 2] g=[0 g]=[0, 6, -1, 2]
h=f+g=[9, 1, 2, 9] 2019/4/26 Puff! The magic dragon, live by the sea…
74
Polynomial algebra f(x)g(x)=? ?f=[9, -5, 3, 7] ?g=[6, -1, 2]
?product= conv(f,g) product = 2019/4/26 Puff! The magic dragon, live by the sea…
75
Polynomial algebra f(x)/g(x)=? ?f=[9, -5, 3, 7] ?g=[6, -1, 2]
?[quotient, remainder]=deconv(f,g) quotient = %商式 remainder = %餘式 2019/4/26 Puff! The magic dragon, live by the sea…
76
Polynomial algebra f(0), f(2), f(4), f(6), f(8), f(10) f(x)/g(x)=?
?f=polyval(a, x); or f=polyval([9, -5, 3, 7], [0:2:10]) 2019/4/26 Puff! The magic dragon, live by the sea…
77
Self testing P. 108 2019/4/26 Puff! The magic dragon, live by the sea…
78
2019/4/26 Puff! The magic dragon, live by the sea…
79
2019/4/26 Puff! The magic dragon, live by the sea…
80
2019/4/26 Puff! The magic dragon, live by the sea…
81
Summary pp.109 Home work Puff! The magic dragon, live by the sea…
2019/4/26 Puff! The magic dragon, live by the sea…
82
2019/4/26 Puff! The magic dragon, live by the sea…
83
2019/4/26 Puff! The magic dragon, live by the sea…
Similar presentations