1 / 83

Array and Matrix Operations in Chapter 2

Learn about array and matrix operations, including addressing, transposing, and combining matrices. Generate evenly spaced large vectors using linspace and logspace functions.

Download Presentation

Array and Matrix Operations in Chapter 2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 第 2 章 陣列(Array)與矩陣(Matrix)的運算 Puff! The magic dragon, live by the sea…

  2. Puff! The magic dragon, live by the sea…

  3. 列向量 & 行向量 • Row vector 列向量 [5, 7, 2]; [3, 4, -6, 7] • Column vector 行向量 Puff! The magic dragon, live by the sea…

  4. 陣 列 Arrays Puff! The magic dragon, live by the sea…

  5. 轉置 transpose Puff! The magic dragon, live by the sea…

  6. Transpose matrix • g=[1;2;3;4]; g1=g’; or g=[1 2 3 4]; 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 k=[1, -9, 1, 0, 3; -4, 4, 1, 1, 2; 24, 2, 2, 36, 2] or k=[1 -9 1 0 3 -4 4 1 1 2 24 2 2 36 2] • k1=k’ 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 = • -3 -2 -1 0 1 2 Puff! The magic dragon, live by the sea…

  9. 產生等間距大向量 ?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 Puff! The magic dragon, live by the sea…

  10. 產生等間距大向量 ?zzz=[3:-0.5:0] zzz = 3.0000 2.5000 2.0000 1.5000 1.0000 0.5000 0 Puff! The magic dragon, live by the sea…

  11. linespace(x1,x2,n)產生等間距列向量 • linespace(5,8,31) • the same as [5:0.1:8] • n表示列向量內含的元素個數 如果省略則內定值為1 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, 0.4642,2.1544, 10.0] Puff! The magic dragon, live by the sea…

  13. 2-Dimensional Arrays Puff! The magic dragon, live by the sea…

  14. 合併 矩陣(行數增加) ?A=[2,4,10; 16,3,7] A = 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需具有相同的列數 Puff! The magic dragon, live by the sea…

  15. 合併 矩陣(列數增加) ?A=[2,4,10; 16,3,7] A = 2 4 10 16 3 7 ?r=[1,2,3] r = 1 2 3 ?B=[A; r] B = 2 4 10 16 3 7 1 2 3 矩陣A與r需具有相同的行數 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 = 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 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 4 10 16 0 7 1 2 3 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(:) ans = 1 2 3 4 5 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 = • 1 2 3 4 5 • ?V(2:4) • ans = • 2 3 4 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) ans = 11 8 4 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 = 5 11 1 8 3 4 Puff! The magic dragon, live by the sea…

  22. 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 Puff! The magic dragon, live by the sea…

  23. ?A=[6,9,4;1,5,7] A = 6 9 4 1 5 7 ?A(1,5)=3 A = 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 Puff! The magic dragon, live by the sea…

  24. B = 3 0 4 9 6 0 0 7 5 1 C = -4 12 3 5 8 ?B(2,:)=C B = 3 0 4 9 6 -4 12 3 5 8 Puff! The magic dragon, live by the sea…

  25. ?D=[3,8,5;2,-6,9] D = 3 8 5 2 -6 9 ?E=D([2,2,2,1],:)%將陣列D的第二列重複三次,第一列重複一次 E = 2 -6 9 2 -6 9 2 -6 9 3 8 5 Puff! The magic dragon, live by the sea…

  26. Using ‘clear’ to avoid errors • clear • clear A 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 = 6 9 7 0 3 k = % index 1 1 2 1 1 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…

  29. ?A=[6 2;-10 -5; 3 0] A = 6 2 -10 -5 3 0 ?max(A) ans = 6 2 ?min(A) ans = -10 -5 ?size(A) ans = 3 2 ?length(A) %傳回m*n (3*2)中的最大值 ans = 3 Puff! The magic dragon, live by the sea…

  30. ?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) ans = 3.0000 + 4.0000i 0 Puff! The magic dragon, live by the sea…

  31. A = 6.0000 2.0000 -10.0000 -5.0000 3.0000 + 4.0000i 0 ?sum(A) %將同一行中的所有元素加總 ans = -1.0000 + 4.0000i -3.0000 Puff! The magic dragon, live by the sea…

  32. A = 6.0000 2.0000 -10.0000 -5.0000 3.0000 + 4.0000i 0 ?sort(A) %依同行中元素大小順序排列 ans = 3.0000 + 4.0000i 0 6.0000 2.0000 -10.0000 -5.0000 Puff! The magic dragon, live by the sea…

  33. Puff! The magic dragon, live by the sea…

  34. Puff! The magic dragon, live by the sea…

  35. Puff! The magic dragon, live by the sea…

  36. Magnitude, length, and absolute value of a Vector • Self testing p.79 Puff! The magic dragon, live by the sea…

  37. 多維陣列multidimensional arrays • m*n*q • m:列(row), n:行(column), q:頁(page) • 第n頁的所有元素 A(:,:,n) Puff! The magic dragon, live by the sea…

  38. ?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) = 4 6 1 5 8 0 3 9 2 A(:,:,2) = 6 2 9 0 3 1 4 7 5 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) C(:,:,1) = 8 2 9 5 C(:,:,2) = 4 6 7 3 Puff! The magic dragon, live by the sea…

  40. cat(1,A,B)=[A;B] 一維 A = 8 2 9 5 B = 4 6 7 3 ?cat(1,A,B) ans = 8 2 9 5 4 6 7 3 ?[A;B] ans = 8 2 9 5 4 6 7 3 Puff! The magic dragon, live by the sea…

  41. cat(2,A,B)=[A,B] 二維 A = 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 Puff! The magic dragon, live by the sea…

  42. cat(3,A,B) 三維 A = 8 2 9 5 B = 4 6 7 3 ?cat(3,A,B) ans(:,:,1) = 8 2 9 5 ans(:,:,2) = 4 6 7 3 ?C(2,1,1) ans = 9 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 ans = -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 ans = 2.6667 0.6667 3.0000 1.6667 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%(A.\B) ans = 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) ans = -0.3333 1.3333 0.2667 1.1333 A = 8 2 9 5 B = 4 6 7 3 • ?A*B ans = 46 54 71 69 ?A.*B ans = 3212 63 15 Puff! The magic dragon, live by the sea…

  45. Puff! The magic dragon, live by the sea…

  46. Puff! The magic dragon, live by the sea…

  47. Puff! The magic dragon, live by the sea…

  48. Take a rest! Try p. 84 ex.2.3-1 Puff! The magic dragon, live by the sea…

  49. ?x'*y ans = -14 6 -16 -28 12 -32 35 -15 40 ?x*y' ans = 38 ?x=[2,4,-5]; y=[-7,3,-8] ?x.*y ans = -14 12 40 ?x*y ??? Error using ==> * Inner matrix dimensions must agree. Puff! The magic dragon, live by the sea…

  50. p.86, ex.2.3-2 ?v=w-r v = -75 23 -10 ?dist2=sqrt(sum(v.*v)) dist2 = 79.0822 ?r=[55,36,25]; ?w=[-20,59,15]; ?dist1=sqrt(sum(r.*r)) dist1 = 70.327 Puff! The magic dragon, live by the sea…

More Related