前言
Matlab常用命令。
一、基本操作与矩阵输入
1.数据类型
logical
char
numeric:
int8、uint8、single、double
int16、uint16、
int32、uint32、
int64、uint64
默认数据类型double
cell
struct
2.优先级
Variable
Built-in function
SubFunction
Private function:
MEX-file
P-file
M-file
3.Format
Style | Example |
---|---|
short | 3.1416 |
long | 3.141592653589793 |
shortE | 3.1416e+00 |
longE | 3.141592653589793e+00 |
bank | 3.14 |
hex | 400921fb54442d18 |
rat | 355/113 |
4.Array Indexing
索引从1开始计数。
A = [1 21 6;
5 17 9;
31 2 7]
A(8) = 9
A([1 3 5]) = 1 31 17
A([1 3;1 3]) = 1 31;1 31
A(3,2) = 2
A([1 3],[1 3]) = 1 6;31 7
5.Colon Operator
1到100
A = 1:100
6.特殊矩阵
eye(n):对角线矩阵
zeros(n1,n2):n1n2零矩阵
ones(n1,n2):n1n2全1矩阵
diag():diagonal矩阵
rand():随机矩阵
7.矩阵相关函数
max(A)
max(max(A))
min(A)
sum(A)
mean(A)
sort(A)
sortrows(A)
size(A)
length(A)
find(A)
二、结构化程序和自定义函数
1.Flow Control
if,elseif,else | Execute statements if condition is true |
---|---|
for | Execute statements specified number of times |
switch,case,otherwise | Execute one of several groups of statements |
try,catch | Execute statements and catch resulting errors |
while | Repeat execution of statements while condition is true |
break | Terminate execution of for or while loop |
continue | Pass control to next iteration of for or while loop |
end | Terminate block of code,or indicate last array index |
pause | Halt executionnn temporarily |
return | Return control to invoking function |
2.相关逻辑运算
Opeerator | Meaning |
---|---|
< | Less than |
<= | Less than or equal to |
> | Greater than |
>= | Greater than or equal to |
== | Equal to |
~= | Not Equal to |
&& | And |
ll | Or |
3.if elseif else
if condition1
statement1
elseif condition2
statement2
else
statement3
end
4.switch
switch expression
case value1
statement1
case value2
statement2
.
.
otherwise
statement
end
5.while
while expression
statement
end
6.for
for variable= start:increment:end
commands
end
7.break
在while中直接跳出循环到达end
x = 2;k = 0;error = inf;
error_threshold = 1e-32;
while error > error_threshold
if k > 100
break
end
x = x - sin(x