作者:蔡承佑
https://matlab.mathworks.com (IDE)
https://drive.matlab.com (folder)
儲存之前寫過的指令
看指令如何使用
ctrl + c
一般變數 : Capital letter is lower case
Matrix : Capital letter is upper case
設立紅色個flag,F10可以執行下一步
script (.m)
有點像是(.py)一次執行scripts內的全部指令
help [command]
showing how to use that command
加上…
會產生.mat檔案
儲存包括變數
%
; 執行但不會輸出
, 執行且輸出(不terminate)
產生新的圖表
edit
點選section -> 按 Run Section 就會執行 section 的 code
可以點擊publish
可以寫在.m檔內
imshow('[image path]')
-method 2
pretty_picture = imread('[image path]');
image(pretty_picture)
axis off
inf
-inf
sqrt(2)
plus(number1, number2)
^
0.0000 + 1.0000i
tic;
…
toc
t0 = cputime; f(8); cputime - t0
*scalars also known as number
key word:
x = 1 4 7
y =
1
4
7
z =
1 4 7
2 8 6
Empty matrix
x = []
初始化某一列某一行
matlab 第一個是1不是0
X(row_id, coloumn_id)
X =
1 2 3 4
5 6 7 8
9 10 11 12
ans = 7
Index exceeds matrix dimensions
X =
1 2 3 4 0
5 6 7 8 0
9 10 11 12 0
0 0 0 0 456
ans = 5 7
array_name = begin : 遞增多少(沒有這個欄位就是1) : end(包括)
x = 1 4 7 10
x = 1 2 3 4
ans = 1 2 3 4 5 6 7
ans = 2 3
H =
1 2 3
4 5 6
G =
1 4
2 5
3 6
ans =
1
3
5
如果有常數 則是每個元素都跟這個常數進行四則運算
矩陣乘法
rem(x, y), x 為被除數 y 為除數
輸入形式或數量不同但名稱相同的function
利用 nargin 跟 nargout 達成
varargin 無限多輸入
放在最後一個input
varargout 無限多輸出
function <回傳值> = <function_name>(<input>)
…
end
global <variable_name>
宣告跟初始化要分開
是一種local variable在呼叫第二次函式時不會改變,除非使用clear <function_name>
可以中斷function藉由return
Matrix Building Function
diag([7 3 9 1])
ans =
7 0 0 0
0 3 0 0
0 0 9 0
0 0 0 1
rand(m, n)
範圍是 0 到 1
randi(imax, m, n)
用途: 生成均勻分布的隨機整數。
imax: 指定生成的隨機整數的最大值,隨機整數將在 1 到 imax 之間(包括 imax)。
m 和 n: 指定生成的隨機整數矩陣的尺寸。
randn(m, n)
用途: 生成服從標準正態分布(均值為 0,標準差為 1)的隨機實數。
m 和 n: 指定生成的隨機數矩陣的尺寸。
判斷function
isscalar()
isempty()
function 內還有一個 function
inner function 的 variable, outer function不會有 (local scope)
outer function 的 variable, inner function會有 (non-local scope)
可以用在cell但不行用在array
也可以多個變數
xfn = @(x, y) x+y
random 的生成跟 rng的值有關
所以每次重新打開輸入rand的值會是一樣的
想要更改可以改動rng => rng('shuffle')
<variable_name> = input('<statement>');
用法像是 C 的 printf 包刮 %d %5.2f 特別的是可以為矩陣
如果輸出的為特殊字符,例如: ' 、 % 、
則打兩次 '' 、 %% 、 \
sprintf()將element 指定給變數
如果在一個表格輸入plot會蓋掉原本的圖,如果不想要蓋掉可以用hold
hold on
…
hold off
help plot
doc plot
figure(表格名稱)
close [表格名稱]
close all
grid on
grid off
xlabel('[label name]')
ylabel('[label name]')
title('[tile of plot]')
axis([[x軸開頭], [x軸結束], [y軸開頭], [y軸結束]])
ans = 3
bar([x vector], [y vector])
hist(<vector_name>)
Non-zero: true
Zero: false
MATLAB returns 1 for true
可以用在matrix上
ans =
0 1 1 0 1
ans =
1 1 0 0 1
ans = 2
Array (& | )
1:5可以是任何array
雙重迴圈 (matrix 遍歷)
quit loop immediately
只會break inner loop 可善用flag
index 的地方由 true/false 的 array 判斷
判斷array的元素是否為true
holmes =
1 1 0 0 1 1
如果是二維陣列呢?
因為陣列是column major
因為是column major改是由上往下 再由左往右
提前 allocate ,使的初始化時,不會重複宣告
zeros(N, N) 就是在preallocate
out of range
視為double/char array
char => %s
如果要用 == 比較 : char matrix 的 dimension 必須相同
如果要創建string 的 matrix 長度必須相同,否則會遇到vercat(內建函式比較string)
findstr(string, <want_to_find_what_string>)
strcmp(str1, str2), 1 is true, 0 is false
strcmpi(str1, str2), i means ignoring case
str2num(string)
num2str(number)
sprintf()將element 指定給變數
strrep(string, char1, char2)把string中的char1改成char2
circshift
Caesear 加密法
extractBetween(s, 3, 8) 對付當s為string而非char array
extractBetween(s, str1, str2) 取str1 str2之間的string
若要像 array 一樣對 string 操作,那他必須採用 char array 的方式表示('') 而不是 string ("")
可以用 string() 、 char() 做切換
其他type轉string容易 => string()
但string轉其他type難 => 除了double() => 先轉double再轉其他type
string(pi)
ans = 3.14
double("pi")
ans = NaN
double("17")
ans = 17
double('17')
ans = 49 55
每個struct內的元素擁有相同field,但也僅限於第一層
account(2)的owner內可能有不同元素
struct can be an array
account(2).number = 1
account(2).name = 'Charlie'
account %查看所有的field
isfield(struct, 'field')
struct = rmfield(struct, 'field')
struct = struct(…)
pointer
()可以視為指標的值
{row, col}可以視為指標指向的值
跟 char matrix 不同,每個元素不用等長了
c{1} is number c(1) is cell
HOME -> Preferences -> Command Window
datetime(year, month, day)
datetime(year, month, day, hour, month, second)
timezones 看時區
format
calyears(整數)=>直接加一年,years()=>事實上是加365.2425天
所以+calyears(1)還會是同一天,但+years(1)就不一定了
可以用在cell但不行用在array
show current path (linux format)
Windows format => ['C:', strrep(pwd, '/', '')]
get into directory
mkdir(file_name)
rmdir(file_name)
儲存目前變數於.mat
load .mat檔
可以決定load哪些
loat .mat檔的名稱 變數名 變數名
[num, text, raw] = xlsread("path")
class(num) = double
class(text) = string
class(raw) = cell
他們都可以用 pointer 或 array 的方式處理
[num, string] = xlsread("path")
[~, ~, everything] = xlsread("path")
tmep 可以用 ~ 代替指讀取string