作者:蔡承佑
確認是否為整數
num == floor(num)
不同的type做運算,最後結果會是容量最小的type
x/n得到0 因為 int16 沒辦法有小數點的數字
不能用 / 只能用 \
*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
visualize
MATLAB會找尋最接近三個解的點,而不是任一個正確的解
*cond
bigger worse
Scripts have no workspace of their own
Scripts have no input or output arguments
INSERT > Table of Contents
出現到每個section的link
ctrl + shift + e
save 可以選擇匯出的檔
一樣可以藉由 publish 變成 live script ,figure要Insert
可以善用 error function 跟 warning function
MATLAB:badsubscript
超過有限範圍
assert(x >= 0)
如果 x < 0 就會跳出 Assertion failed.
Elapsed time is 0.155401 seconds.
t = 0.1412
complexity (更多在演算法)
用來check function 是否相同
isequal(rooting_v3(v, w), rooting_v5(v, w))
% 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
第一個較好
preallocation
replace explicit looping
stop recursion
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
列儲存 column major
matlab儲存方式是列優先
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 快很多,因為他是用列來儲存,雖然最後還要轉至矩陣
VeryCoolGuy = struct('name', [])