MATLAB Matlab Operator Name Comments and Examples Array
MATLAB介紹
Matlab介面
矩陣運算 • Operator • + • − • . * • * • . / • . • / Name Comments and Examples Array and matrix addition a + b, A + B, or a + A. Array and matrix subtraction a − b, A − B, A − a, . Array multiplication Cv= A. *B, C(I, J) = A(I, J)*B(I, J). Matrix multiplication A*B, standard matrix multiplication. Array right division† C = A. /B, C(I, J) = A(I, J)/B(I, J). Array left division† C = A. B, C(I, J) = B(I, J)/A(I, J). Matrix right division A/B is the preferred way to compute A*inv(B).
矩陣運算 • Operator • Name Comments and Examples Matrix left division AB is the preferred way to compute inv(A)*B. Array power If C = A. ^B, then C(I, J) = A(I, J)^B(I, J). • . ^ • . ' matrix transpose A. ', standard matrix transpose. • • • ' + − matrix transpose Unary plus Unary minus A', standard matrix transpose. +A is the same as 0 + A. −A is the same as 0 − A or − 1*A.
If Else • 用法: if (True or False), 計算, end ex: if (isempty(t)) t = 0; End • 用法: if (True_or_False), 計算, else, 計算, end
switch • 用法: switch (switch_expr) case_expr_1 statement 1 otherwise statement 1 end ex: switch (value) case {1 , 3 , 5 , 7 , 9} disp(‘the value is odd. ’); case {2 , 4 , 6, 8, 10} disp(‘the value is even. ’); otherwise disp(‘the value is out of range. ’); end
For loop • 用法:for 變數 = 向量或序列, 計算, end ex: for i = 1: 100, ex: for i=1: 2: 100 , y(i) =2; s = s+i; End ex: for x = [0 1 5 3], disp(x); //印出 x end
While loop • 用法:while expression , 計算, end ex: i=1; ex: i=1 while i <= 100 y(i) =2; s = s+i; i=i+1; i=i+2; end
MATLAB 程式編寫 • 函數語法---函數檔案會以 function開頭 第一行定義函數名稱(lookfor),接下來幾行 是說明檔(help),最後是指令碼 。如下所示: function num=countpos(a) % countpos finds the number of positive % elementsin a matrix. % usage: % n=countpos(a) num=sum(a(: )>0);
• 参考網站 http: //libai. math. ncu. edu. tw/bcc 16/B/matlab /index. shtml
- Slides: 34