# 初階 Matlab 2
>作者:蔡承佑
:::spoiler Skill
確認是否為整數
num == floor(num)
:::
:::spoiler Binary Arithmetic Operations


>
==不同的type做運算,最後結果會是容量最小的type==
>
>x/n得到0 因為 int16 沒辦法有小數點的數字
>
:::
# Linear Equation
:::spoiler 有解

==不能用 / 只能用 \\==
*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)

visualize
>
:::
:::spoiler 無解 (3 equation 2 variables)

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

*cond
:::
:::spoiler 預估值error
bigger worse

:::
# 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

:::
:::spoiler Table of contents
INSERT > Table of Contents
出現到每個section的link
>
:::
:::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)

:::
:::spoiler Exception

>MATLAB:badsubscript
>超過有限範圍


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

:::
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
```
>
:::
:::spoiler 減少同樣的code重複被執行 (多重迴圈)
第一個較好
>
>
:::
==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


:::
:::spoiler repmat

:::
# Index Re-ordering
列儲存 ==column major==
matlab儲存方式是列優先

:::spoiler

:::
:::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

# oop
:::spoiler struct
```matlab=
VeryCoolGuy = struct('name', [])
```
:::