初階 Matlab 2

作者:蔡承佑

Skill

確認是否為整數
num == floor(num)

Binary Arithmetic Operations

螢幕擷取畫面 2024-06-13 214955

螢幕擷取畫面 2024-06-13 215754

螢幕擷取畫面 2024-06-13 214930

不同的type做運算,最後結果會是容量最小的type

螢幕擷取畫面 2024-06-13 220433
x/n得到0 因為 int16 沒辦法有小數點的數字
螢幕擷取畫面 2024-06-13 220554

Linear Equation

有解

螢幕擷取畫面 2024-06-13 231455

不能用 / 只能用 \
*mldivide

check error

A = rand(20, 20); b = rand(20, 1); x = A\b; e = A*x-b; max(abs(e))

ans = 8.8818e-16

無解 (2 equation 2 variables)

螢幕擷取畫面 2024-06-13 232103

visualize

螢幕擷取畫面 2024-06-13 232328

無解 (3 equation 2 variables)

螢幕擷取畫面 2024-06-13 232846

MATLAB會找尋最接近三個解的點,而不是任一個正確的解

Underdetermined Equations

螢幕擷取畫面 2024-06-13 233513
*cond

預估值error

bigger worse
螢幕擷取畫面 2024-06-13 234251

Live Script

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

fit function

螢幕擷取畫面 2024-06-14 111824

Table of contents

INSERT > Table of Contents

出現到每個section的link

螢幕擷取畫面 2024-06-14 112135

Equation

ctrl + shift + e

save

save 可以選擇匯出的檔

the old way script

一樣可以藉由 publish 變成 live script ,figure要Insert

Error Handline

可以善用 error function 跟 warning function

Exception Handline (try catch end)

螢幕擷取畫面 2024-06-14 121505

Exception

螢幕擷取畫面 2024-06-14 134439

MATLAB:badsubscript
超過有限範圍

螢幕擷取畫面 2024-06-14 135010
螢幕擷取畫面 2024-06-14 135211

  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
assert

assert(x >= 0)

如果 x < 0 就會跳出 Assertion failed.

Algorithmic complexity

measure time
  1. tic toc
    tic; function(); toc

Elapsed time is 0.155401 seconds.

  1. timeit
    t = timeit(@() function())

t = 0.1412

fibonacci

螢幕擷取畫面 2024-06-14 145024

complexity (更多在演算法)

isequal

用來check function 是否相同

isequal(rooting_v3(v, w), rooting_v5(v, w))

efficiency

性能分析 (profile)
% 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

減少同樣的code重複被執行 (多重迴圈)

第一個較好

螢幕擷取畫面 2024-06-14 155923

螢幕擷取畫面 2024-06-14 155950

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 的概念

rv = nthroot(v, 1:length(v));
for ii = 1:length(v) rv(ii) = nthroot(v(ii), ii); end
example nthroot

螢幕擷取畫面 2024-06-14 155923

螢幕擷取畫面 2024-06-14 164704

repmat

螢幕擷取畫面 2024-06-14 172123

Index Re-ordering

列儲存 column major
matlab儲存方式是列優先

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

螢幕擷取畫面 2024-06-14 174249

example

version1

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

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

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

oop

struct
VeryCoolGuy = struct('name', [])