第 2 章 陣列(Array)與矩陣(Matrix)的運算 2019/4/26 Puff! The magic dragon, live by the sea…
2019/4/26 Puff! The magic dragon, live by the sea…
列向量 & 行向量 Row vector 列向量 [5, 7, 2]; [3, 4, -6, 7] Column vector 行向量 2019/4/26 Puff! The magic dragon, live by the sea…
陣 列 Arrays 2019/4/26 Puff! The magic dragon, live by the sea…
轉置 transpose 2019/4/26 Puff! The magic dragon, live by the sea…
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…
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…
產生等間距大向量 x=[m:q:n] ?x=[0:2:8] x = 0 2 4 6 8 ?y=[-3:2] y = 0 2 4 6 8 ?y=[-3:2] y = -3 -2 -1 0 1 2 2019/4/26 Puff! The magic dragon, live by the sea…
產生等間距大向量 ?z=[0:0.1:2] z = Columns 1 through 7 0 0.1000 0.2000 0.3000 0.4000 0.5000 0.6000 Columns 8 through 14 0.7000 0.8000 0.9000 1.0000 1.1000 1.2000 1.3000 Columns 15 through 21 1.4000 1.5000 1.6000 1.7000 1.8000 1.9000 2.0000 2019/4/26 Puff! The magic dragon, live by the sea…
產生等間距大向量 ?zzz=[3:-0.5:0] zzz = 3.0000 2.5000 2.0000 1.5000 1.0000 0.5000 0 2019/4/26 Puff! The magic dragon, live by the sea…
linespace(x1,x2,n) 產生等間距列向量 the same as [5:0.1:8] n 表示列向量內含的元素個數 如果省略則內定值為1 2019/4/26 Puff! The magic dragon, live by the sea…
logspace(x1,x2,n) 產生具有對數等間距的向量 logspace (a, b, n) 在10a~10b間(含)取的點數 logspace(-1,1,4)=[0.1, 0.4642,2.1544, 10.0] 2019/4/26 Puff! The magic dragon, live by the sea…
2-Dimensional Arrays Puff! The magic dragon, live by the sea… 2019/4/26 Puff! The magic dragon, live by the sea…
合併 矩陣(行數增加) 矩陣A與r需具有相同的列數 ?A=[2,4,10; 16,3,7] A = 2 4 10 16 3 7 2 4 10 16 3 7 ?r=[1,1;0,0] r = 1 1 0 0 ?B=[A r] B = 2 4 10 1 1 16 3 7 0 0 矩陣A與r需具有相同的列數 2019/4/26 Puff! The magic dragon, live by the sea…
合併 矩陣(列數增加) 矩陣A與r需具有相同的行數 ?A=[2,4,10; 16,3,7] A = 2 4 10 16 3 7 2 4 10 16 3 7 ?r=[1,2,3] r = 1 2 3 ?B=[A; r] B = 矩陣A與r需具有相同的行數 2019/4/26 Puff! The magic dragon, live by the sea…
Transpose matrix (complex conjugate) ?A=[3+2*i, 2+6*i; 5+4*i, 3+5*i] A = 3.0000 + 2.0000i 2.0000 + 6.0000i 5.0000 + 4.0000i 3.0000 + 5.0000i ?A' ans = 3.0000 - 2.0000i 5.0000 - 4.0000i 2.0000 - 6.0000i 3.0000 - 5.0000i 2019/4/26 Puff! The magic dragon, live by the sea…
Array addressing定址 修改矩陣內元素 D(1,3)=6 B = 2 4 10 16 3 7 1 2 3 ?B(2,2)=0 2 4 10 16 3 7 1 2 3 ?B(2,2)=0 16 0 7 2019/4/26 Puff! The magic dragon, live by the sea…
Array addressing定址 2. V(:) V向量內所有元素 ?V=[1 2 3 4 5] V = 1 2 3 4 5 ?V(:) 1 2 3 4 5 ?V(:) ans = 1 2 3 4 5 2019/4/26 Puff! The magic dragon, live by the sea…
Array addressing定址 3. V(2:4) V(2), V(3), V(4), V(5), V(6) V = 1 2 3 4 5 ?V(2:4) ans = 2 3 4 2019/4/26 Puff! The magic dragon, live by the sea…
Array addressing定址 A(:,3) 矩陣A第三行所有的元素 A = 3 5 11 17 1 8 2 3 4 ?A(:,3) 3 5 11 17 1 8 2 3 4 ?A(:,3) ans = 11 8 4 2019/4/26 Puff! The magic dragon, live by the sea…
Array addressing定址 5. A(:,2:3) 矩陣A第2行至第3行所有的元素 A = 3 5 11 17 1 8 2 3 4 3 5 11 17 1 8 2 3 4 ?A(:,2:3) ans = 5 11 1 8 3 4 2019/4/26 Puff! The magic dragon, live by the sea…
Array addressing定址 6. A(2:3,1:3)矩陣A第2至第3列,第1至第3行所有的元素 (可用以分割矩陣) A = 3 5 11 17 1 8 2 3 4 ?A(2:3,2:3) ans = 1 8 3 4 2019/4/26 Puff! The magic dragon, live by the sea…
?B=A(:,5:-1:1) %把A陣列行排列的次序顛倒過來 B = 3 0 4 9 6 0 0 7 5 1 6 9 4 1 5 7 ?A(1,5)=3 6 9 4 0 3 1 5 7 0 0 ?B=A(:,5:-1:1) %把A陣列行排列的次序顛倒過來 B = 3 0 4 9 6 0 0 7 5 1 2019/4/26 Puff! The magic dragon, live by the sea…
B = 3 0 4 9 6 0 0 7 5 1 C = -4 12 3 5 8 ?B(2,:)=C 2019/4/26 Puff! The magic dragon, live by the sea…
?E=D([2,2,2,1],:) %將陣列D的第二列重複三次,第一列重複一次 E = 3 8 5 2 -6 9 ?E=D([2,2,2,1],:) %將陣列D的第二列重複三次,第一列重複一次 E = 2019/4/26 Puff! The magic dragon, live by the sea…
Using ‘clear’ to avoid errors clear A 2019/4/26 Puff! The magic dragon, live by the sea…
A = 6 9 4 0 3 1 5 7 0 0 ?max(A) ans = 6 9 7 0 3 ?[x,k]=max(A) x = 6 9 4 0 3 1 5 7 0 0 ?max(A) ans = 6 9 7 0 3 ?[x,k]=max(A) x = k = % index 1 1 2 1 1 2019/4/26 Puff! The magic dragon, live by the sea…
B = 6 9 4 0 3 ?max(B) ans = 9 Puff! The magic dragon, live by the sea… 6 9 4 0 3 ?max(B) ans = 9 2019/4/26 Puff! The magic dragon, live by the sea…
?length(A) %傳回m*n (3*2)中的最大值 3 6 2 -10 -5 3 0 ?max(A) ans = ?min(A) ?size(A) 3 2 ?length(A) %傳回m*n (3*2)中的最大值 3 2019/4/26 Puff! The magic dragon, live by the sea…
?A=[6 2; -10 -5;3+4*i 0] A = 6.0000 2.0000 -10.0000 -5.0000 3.0000 + 4.0000i 0 ?max(A) ans = -10 -5 ?min(A) 2019/4/26 Puff! The magic dragon, live by the sea…
A = 6.0000 2.0000 -10.0000 -5.0000 3.0000 + 4.0000i 0 ?sum(A) %將同一行中的所有元素加總 ans = -1.0000 + 4.0000i -3.0000 2019/4/26 Puff! The magic dragon, live by the sea…
?sort(A) %依同行中元素大小順序排列 ans = 6.0000 2.0000 -10.0000 -5.0000 3.0000 + 4.0000i 0 ?sort(A) %依同行中元素大小順序排列 ans = 2019/4/26 Puff! The magic dragon, live by the sea…
2019/4/26 Puff! The magic dragon, live by the sea…
2019/4/26 Puff! The magic dragon, live by the sea…
2019/4/26 Puff! The magic dragon, live by the sea…
Magnitude, length, and absolute value of a Vector Self testing p.79 2019/4/26 Puff! The magic dragon, live by the sea…
多維陣列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…
?A=[4,6,1;5,8,0;3,9,2] A = 4 6 1 5 8 0 3 9 2 ?A(:,:,2)=[6,2,9;0,3,1;4,7,5] A(:,:,1) = A(:,:,2) = 6 2 9 0 3 1 4 7 5 2019/4/26 Puff! The magic dragon, live by the sea…
?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) 8 2 9 5 ?B=[4 6; 7 3] B = 4 6 7 3 ?C=cat(3,A,B) C(:,:,1) = C(:,:,2) = 2019/4/26 Puff! The magic dragon, live by the sea…
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 8 2 9 5 B = 4 6 7 3 ?cat(1,A,B) ans = ?[A;B] ans = 8 2 9 5 4 6 7 3 2019/4/26 Puff! The magic dragon, live by the sea…
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 8 2 9 5 B = 4 6 7 3 ?cat(2,A,B) ans = 8 2 4 6 9 5 7 3 ?[A,B] ans = 8 2 4 6 9 5 7 3 2019/4/26 Puff! The magic dragon, live by the sea…
cat(3,A,B) 三維 A = ans(:,:,2) = 8 2 4 6 9 5 7 3 B = 4 6 7 3 ?C(2,1,1) 8 2 9 5 B = 4 6 7 3 ?cat(3,A,B) ans(:,:,1) = ans(:,:,2) = 4 6 7 3 ?C(2,1,1) ans = 9 2019/4/26 Puff! The magic dragon, live by the sea…
Array Operations B = 4 6 7 3 ?A+B ans = 12 8 16 8 ?A-3*B -4 -16 -12 -4 4 6 7 3 ?A+B ans = 12 8 16 8 ?A-3*B -4 -16 -12 -4 A = 8 2 9 5 ?3*A ans = 24 6 27 15 ?A+1000 ans = 1008 1002 1009 1005 ?A/3 2.6667 0.6667 3.0000 1.6667 2019/4/26 Puff! The magic dragon, live by the sea…
Array Operations ?A./B %(B.\A) ans = 2.0000 0.3333 1.2857 1.6667 8 2 9 5 B = 4 6 7 3 ?A*B ans = 46 54 71 69 ?A.*B 32 12 63 15 ?A./B %(B.\A) ans = 2.0000 0.3333 1.2857 1.6667 ?B./A %(A.\B) 0.5000 3.0000 0.7778 0.6000 ?A\B %(A-1*B) ans = 0.2727 1.0909 0.9091 -1.3636 ?A/B %(A*B-1) -0.3333 1.3333 0.2667 1.1333 2019/4/26 Puff! The magic dragon, live by the sea…
2019/4/26 Puff! The magic dragon, live by the sea…
2019/4/26 Puff! The magic dragon, live by the sea…
2019/4/26 Puff! The magic dragon, live by the sea…
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…
Inner matrix dimensions must agree. ?x'*y ans = -14 6 -16 -28 12 -32 35 -15 40 ?x*y' 38 ?x=[2,4,-5]; y=[-7,3,-8] ?x.*y ans = -14 12 40 ?x*y ??? Error using ==> * Inner matrix dimensions must agree. 2019/4/26 Puff! The magic dragon, live by the sea…
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 = -75 23 -10 ?dist2=sqrt(sum(v.*v)) dist2 = 79.0822 2019/4/26 Puff! The magic dragon, live by the sea…
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…
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 = 54.3689 53.6585 53.8462 52.4752 49.3333 2019/4/26 Puff! The magic dragon, live by the sea…
Array exponentiation ?A=[4 -5;2 3] ?C=3.^A A = C = 4 -5 81.0000 0.0041 4 -5 2 3 ?B=A.^3 B = 64 -125 8 27 ?C=3.^A C = 81.0000 0.0041 9.0000 27.0000 2019/4/26 Puff! The magic dragon, live by the sea…
I=V/R, P=V^2/R ?R=[10000, 20000, 35000, 100000, 200000]; ?V=[120, 80, 110, 200, 350]; ?current=V./R current = 0.0120 0.0040 0.0031 0.0020 0.0018 ?power=V.^2./R power = 1.4400 0.3200 0.3457 0.4000 0.6125 2019/4/26 Puff! The magic dragon, live by the sea…
?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 = 70.0461 x = 0.5219 2019/4/26 Puff! The magic dragon, live by the sea…
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…
0 50.0000 60.0000 70.0000 80.0000 10.0000 2.9940 3.8265 4.5052 4.9482 12.0000 4.3114 5.5102 6.4875 7.1254 14.0000 5.8682 7.5000 8.8302 9.6985 16.0000 7.6646 9.7959 11.5334 12.6674 18.0000 9.7006 12.3980 14.5969 16.0322 20.0000 11.9760 15.3061 18.0209 19.7928 2019/4/26 Puff! The magic dragon, live by the sea…
Ax=b 2019/4/26 Puff! The magic dragon, live by the sea…
2019/4/26 Puff! The magic dragon, live by the sea…
2019/4/26 Puff! The magic dragon, live by the sea…
2019/4/26 Puff! The magic dragon, live by the sea…
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…
2019/4/26 Puff! The magic dragon, live by the sea…
2019/4/26 Puff! The magic dragon, live by the sea…
2019/4/26 Puff! The magic dragon, live by the sea…
Special matrices Null matrix Identity matrix Puff! The magic dragon, 2019/4/26 Puff! The magic dragon, live by the sea…
Special matrices ?eye(3) ?ones(3) ans = ans = 1 0 0 1 1 1 0 1 0 0 0 1 1 0 0 0 1 0 0 0 1 ?ones(3) ans = 1 1 1 2019/4/26 Puff! The magic dragon, live by the sea…
Special matrices ?zeros(3) ?zeros(2,3) ans = ans = 0 0 0 0 0 0 0 0 0 ?zeros(2,3) ans = 0 0 0 2019/4/26 Puff! The magic dragon, live by the sea…
Special products cross(A,B) ~ momentum dot(r,F) 2019/4/26 Puff! The magic dragon, live by the sea…
2019/4/26 Puff! The magic dragon, live by the sea…
2019/4/26 Puff! The magic dragon, live by the sea…
polyminals [a1, a2,… an-1, an, an+1] Puff! The magic dragon, 2019/4/26 Puff! The magic dragon, live by the sea…
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…
Polynomial algebra f(x)g(x)=? ?f=[9, -5, 3, 7] ?g=[6, -1, 2] ?product= conv(f,g) product = 54 -39 41 29 -1 14 2019/4/26 Puff! The magic dragon, live by the sea…
Polynomial algebra f(x)/g(x)=? ?f=[9, -5, 3, 7] ?g=[6, -1, 2] ?[quotient, remainder]=deconv(f,g) quotient = %商式 1.5000 -0.5833 remainder = %餘式 0 0 -0.5833 8.1667 2019/4/26 Puff! The magic dragon, live by the sea…
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…
Self testing P. 108 2019/4/26 Puff! The magic dragon, live by the sea…
2019/4/26 Puff! The magic dragon, live by the sea…
2019/4/26 Puff! The magic dragon, live by the sea…
2019/4/26 Puff! The magic dragon, live by the sea…
Summary pp.109 Home work Puff! The magic dragon, live by the sea… 2019/4/26 Puff! The magic dragon, live by the sea…
2019/4/26 Puff! The magic dragon, live by the sea…
2019/4/26 Puff! The magic dragon, live by the sea…