# 初階 Matlab 2 >作者:蔡承佑 :::spoiler Skill 確認是否為整數 num == floor(num) ::: :::spoiler Binary Arithmetic Operations ![螢幕擷取畫面 2024-06-13 214955](https://hackmd.io/_uploads/Bk079OOrR.png) ![螢幕擷取畫面 2024-06-13 215754](https://hackmd.io/_uploads/S1Hxsu_rC.png) >![螢幕擷取畫面 2024-06-13 214930](https://hackmd.io/_uploads/S1CgKu_BA.png) ==不同的type做運算,最後結果會是容量最小的type== >![螢幕擷取畫面 2024-06-13 220433](https://hackmd.io/_uploads/HJAun_OBA.png) >x/n得到0 因為 int16 沒辦法有小數點的數字 >![螢幕擷取畫面 2024-06-13 220554](https://hackmd.io/_uploads/S1kRndOH0.png) ::: # Linear Equation :::spoiler 有解 ![螢幕擷取畫面 2024-06-13 231455](https://hackmd.io/_uploads/Sk3ZTt_HR.png) ==不能用 / 只能用 \\== *mldivide check error ```matlab= A = rand(20, 20); b = rand(20, 1); x = A\b; e = A*x-b; max(abs(e)) ``` >ans = 8.8818e-16 ::: :::spoiler 無解 (2 equation 2 variables) ![螢幕擷取畫面 2024-06-13 232103](https://hackmd.io/_uploads/By5OAFur0.png) visualize >![螢幕擷取畫面 2024-06-13 232328](https://hackmd.io/_uploads/BkAZ1quSC.png) ::: :::spoiler 無解 (3 equation 2 variables) ![螢幕擷取畫面 2024-06-13 232846](https://hackmd.io/_uploads/SySUlcurC.png) >MATLAB會找尋最接近三個解的點,而不是任一個正確的解 ::: :::spoiler Underdetermined Equations ![螢幕擷取畫面 2024-06-13 233513](https://hackmd.io/_uploads/rkY6-qurC.png) *cond ::: :::spoiler 預估值error bigger worse ![螢幕擷取畫面 2024-06-13 234251](https://hackmd.io/_uploads/HkjiQ5dS0.png) ::: # Live Script :::spoiler Scripts VS Functions - Scripts have no workspace of their own - command window - modify existing variables of that workspace - Scripts have no input or output arguments ::: :::spoiler fit function ![螢幕擷取畫面 2024-06-14 111824](https://hackmd.io/_uploads/B1ai8VFr0.png) ::: :::spoiler Table of contents INSERT > Table of Contents 出現到每個section的link >![螢幕擷取畫面 2024-06-14 112135](https://hackmd.io/_uploads/S1-Lw4tBR.png) ::: :::spoiler Equation ctrl + shift + e ::: :::spoiler save save 可以選擇匯出的檔 ::: :::spoiler the old way script 一樣可以藉由 publish 變成 live script ,figure要Insert ::: # Error Handline 可以善用 error function 跟 warning function :::spoiler Exception Handline (try catch end) ![螢幕擷取畫面 2024-06-14 121505](https://hackmd.io/_uploads/ryoR7rtHC.png) ::: :::spoiler Exception ![螢幕擷取畫面 2024-06-14 134439](https://hackmd.io/_uploads/SyXWF8Fr0.png) >MATLAB:badsubscript >超過有限範圍 ![螢幕擷取畫面 2024-06-14 135010](https://hackmd.io/_uploads/Skcmc8FSA.png) ![螢幕擷取畫面 2024-06-14 135211](https://hackmd.io/_uploads/BJ2q58FrA.png) 1. implemented with try-catch block 2. identify errors with catch ME and Exception.last 3. Throw errors with the throw function 4. rethrow errors with the rethrow function 5. assert function to identify incorrect assumptions ::: :::spoiler assert assert(x >= 0) >如果 x < 0 就會跳出 Assertion failed. ::: # Algorithmic complexity :::spoiler measure time 1. tic toc tic; function(); toc >Elapsed time is 0.155401 seconds. 2. timeit t = timeit(@() function()) >t = 0.1412 ::: :::spoiler fibonacci ![螢幕擷取畫面 2024-06-14 145024](https://hackmd.io/_uploads/SJJSdPtHA.png) ::: complexity (更多在演算法) :::spoiler isequal 用來check function 是否相同 >isequal(rooting_v3(v, w), rooting_v5(v, w)) ::: # efficiency :::spoiler 性能分析 (profile) ```matlab= % 1. Turn off any existing profiling profile off % 2. Start profiling profile on % 3. Run the code graph = { [2, 3], % Neighbors of node 1 [1, 3], % Neighbors of node 2 [1, 2, 4], % Neighbors of node 3 [3] % Neighbors of node 4 }; result = max_clique(graph); % 4. Stop profiling profile off % 5. View profiling report profile viewer ``` >![螢幕擷取畫面 2024-06-14 160653](https://hackmd.io/_uploads/Sk24cutSA.png) ::: :::spoiler 減少同樣的code重複被執行 (多重迴圈) 第一個較好 >![螢幕擷取畫面 2024-06-14 155923](https://hackmd.io/_uploads/r16j_dKHA.png) >![螢幕擷取畫面 2024-06-14 155950](https://hackmd.io/_uploads/S1_nd_KHR.png) ::: ==preallocation== ==replace explicit looping== stop recursion # Vectorization Any operator or function that operates on an entire vector or entire array is said to be a vector command 善用 logical indexing 的概念 ```matlab= rv = nthroot(v, 1:length(v)); ``` ```matlab= for ii = 1:length(v) rv(ii) = nthroot(v(ii), ii); end ``` :::spoiler example nthroot ![螢幕擷取畫面 2024-06-14 155923](https://hackmd.io/_uploads/r16j_dKHA.png) ![螢幕擷取畫面 2024-06-14 164704](https://hackmd.io/_uploads/rka57KYrC.png) ::: :::spoiler repmat ![螢幕擷取畫面 2024-06-14 172123](https://hackmd.io/_uploads/Hk-khFtBR.png) ::: # Index Re-ordering 列儲存 ==column major== matlab儲存方式是列優先 ![螢幕擷取畫面 2024-06-14 173551](https://hackmd.io/_uploads/rkrXy5FrR.png) :::spoiler ![螢幕擷取畫面 2024-06-14 174249](https://hackmd.io/_uploads/rJCoe5KSR.png) ::: :::spoiler example version1 ```matlab= function A = not_preallocatable_v1(N) % from COMPUTER PROGRAMMING WITH MATLAB, 3rd Edition, 2015 % by J. M. Fitzpatrick and A. Ledeczi % Chapter 2, Section 4.9 ii = 0; while rand > 1/N ii = ii + 1; for jj = 1:N A(ii,jj) = ii + jj^2; end end ``` version2 ```matlab= function A = not_preallocatable_v2(N) % from COMPUTER PROGRAMMING WITH MATLAB, 3rd Edition, 2015 % by J. M. Fitzpatrick and A. Ledeczi % Chapter 2, Section 4.9 ii = 0; while rand > 1/N ii = ii + 1; for jj = 1:N A(jj,ii) = ii + jj^2; end end A = A'; ``` ==version2 比 version1 快很多,因為他是用列來儲存,雖然最後還要轉至矩陣== ::: # parfor ![螢幕擷取畫面 2024-06-14 174637](https://hackmd.io/_uploads/SkScbqtBR.png) # oop :::spoiler struct ```matlab= VeryCoolGuy = struct('name', []) ``` :::