Week 02
- format compact 命令可以出去 answer 中無用的 horizontal line
format 有很多格式調(diào)整選擇 - 我們可以將 command history 作為一欄拿出來,方便重輸歷史命令
- 通過 ... 我們可以將一個(gè)命令分行寫
- plot()有option可以選空凸,xlabel,ylabel,title,grid off
- axis([x-start贮庞,x-end,y-start陵且,y-end])
bar(x,y,z) 條狀圖
figure 一個(gè)新的figure
pie([p1,p2,p3...]) 餅圖
close(2) close figure 2
close all close all figures
Week 03
command | functionality |
---|---|
size() | 返回 行數(shù)和列數(shù) |
1:3:10 | 從3到10以3遞增的數(shù) 等同于 colon(1,3,10) |
如果開始數(shù)大于結(jié)尾數(shù),就會(huì)導(dǎo)致 empty matrix 1行0列 | [] 是0行0列 |
x(row, col) | for indexing to retrieve or assign a value and when assigning, if x doesn't exist, a new matrix will be created |
x(2,[1,3]) / x(1:2:4) | subarray opeartions: return the value of x(2,1) and x(2,3) |
end | reserved keyword 能夠表示matrix某一行/列的末尾 |
combing matrices | 連接端必須有相同的行/列 |
G=H' | Transpose of matrix |
.* ./ .\ | element-wise multiplication/ division/ division in reverse(Reciprocal) |
矩陣運(yùn)算 | 加減乘除必須滿足行列要求 但如果有一個(gè)是scalar 那就沒有這個(gè)限制 |
Week 04 Function IO
Command | Functionality |
---|---|
x(:) | 將所有矩陣轉(zhuǎn)變?yōu)橐粋€(gè) n*1 的向量 |
sum() | 計(jì)算矩陣每個(gè)列的和 |
function [a, b]=func(x, y) | 函數(shù)的標(biāo)準(zhǔn)格式 |
global var | 可以從 command line 以 global var 的形式訪問全局變量 |
script | 使用 script 的時(shí)候杜顺,變量會(huì)進(jìn)入workspace |
Week 05 Programmer's Toolbox
Command | Functionality |
---|---|
max() | 既可以返回每列最大數(shù)财搁,還可以返回這個(gè)數(shù)的index |
zeros() ones() diag() randn() randi() eye() diag() | matrix building |
fix() | 取整 |
rng(n) rng('shuffle') | 設(shè)置生成隨機(jī)數(shù)的種子 |
input() fprintf() | 輸入輸出 |
hold grid legend | 圖表的設(shè)置 |
Week 06 Selection
Commands | Functionality |
---|---|
if ... elseif ... else ... end | 選擇語句 |
~= | 不等于 |
& && | 前者會(huì)計(jì)算左右兩式,會(huì)將array中每個(gè)成員分別計(jì)算哑舒;后者只有在左式不確定情況下計(jì)算右式妇拯,而且只適用于 scalar value |
nargin nargout | 輸入/輸出參數(shù)的個(gè)數(shù) |
%comment | 如果在函數(shù)聲明后面加注釋,那么 help 命令就能像調(diào)用內(nèi)置函數(shù)一樣調(diào)用自定義函數(shù) |
persistent | 類似于C++的 local static variable |
Robustness
function [table summa] = multable(n, m)
if nargin < 1
error('must have at least one input argument');
end
if nargin < 2
m = n;
elseif ~isscalar(m) || m < 1 || m ~= fix(m)
error('m needs to be a positive integer');
end
if ~isscalar(n) || n < 1 || n ~= fix(n)
error('n needs to be a positive integer');
end
table = (1:n)' * (1:m);
if nargout == 2
summa = sum(table(:));
end
Week 07 Loops
command | functionality |
---|---|
15+3i | 復(fù)數(shù) |
v(logical(v)) | 返回不是0的element洗鸵,這就是 logical indexing |
tick; func(), tok | 計(jì)時(shí) |
preallocation | 提前創(chuàng)建滿足要求的矩陣能有效減少計(jì)算時(shí)間 |
Week 08 Data Types
Command | Functionality |
---|---|
class() whos() | type / detailed info |
double char logical | data types |
isa() | type check |
intmax('uint32') intmin realmax realmin | range check |
iint8() uint32() | conversion |
在string中''表示一個(gè)quote | |
sprintf() fprintf | sprintf()需要一個(gè)output argument來接值 而 sprintf()不需要 |
struct field | 只需要名稱一樣 不需要類型一樣 |
rmfield() | 不改變參數(shù)值越锈,只返回改變值 |
cell | 使用{} |
cell() cell{} | 前者得到指針(cell) double有[]包住 string雖然是一樣 但是不能用==進(jìn)行比較 后者得到內(nèi)容 |
cell pointers | 在matlab中,沒有兩個(gè)cell pointers能指向同一個(gè)object 所以在c++中我們可以通過傳遞指針改變函數(shù)外的值在matlab中行不通膘滨,因?yàn)槲覀儾荒茉诤瘮?shù)參數(shù)里面新建一個(gè)指針指向函數(shù)外的object甘凭,只能指向一個(gè)復(fù)制過后的object |
String Functions
Struct Functions
Cell Functions
Week 09
Command | Description |
---|---|
save + fileName + content in workspace | load fileName |
xslread() xslwrite() | |
~ | tilde可以用來作為必要但我們不需要的output argument |