数据类型
int uint(无符号)
double(默认矩阵变量类型) single
可以通过强制类型转换来更改矩阵变量类型
disp(round(x, -floor(log10(abs(x))) + 2)) 保留3位有效数字
”%.*f “,p,x 保留p位小数
%g 自动适应表示位数
矩阵/列表中的一些操作
为列表赋值 [1 2 3]=[1,2,3]
cell数组 {1,2,'3'} 可以
:=1,maxn
;=前后行连接
(,)=locate ([],[])
a(:) 遍历
a(6) 优先向下数 (区别C++)
[A A(:,end)] 增加列
a' 行列翻转
flipud(A) 上下翻转
fliplr(A) 左右翻转
sum 每列求和
reshape 根据数量重组
numel 数组元素个数
[ [ A ; sum(A) ] [ sum(A,2) ; sum(A(:)) ] ] 输出A以及行列和总和
myStruct = struct('field1', value1, 'field2', value2, ...);
逻辑运算
xor 相同出 0 不同出 1
逻辑运算和C++区别
!= => ~=
if else 使用必须换行
if ***
***
else
***
endSwitch 用法
switch n
case '+', n1+n2
case '-', n1-n2
case '*', n1*n2
case '/', n1/n2
case {'',''},n1+n2+n3...
otherwise, disp('Not a basic arithmetic operation')
end画图
plot
hold on
hold off 之后重画直接清屏
其他函数/语句
fd=fopen(".txt","r");
fclose(fd);
sprintf() 用法同fprintf,返回fprintf 的字符串值
fprintf()
fscanf(fd,"%d",n) 读n个数据
fgetl(fd) 读取一行
exist 不存在:0 变量:1 文件 :2 Mex:3 文件夹:7 (可以help查看具体)
save 保存工作区数据 注意:save("file","variable") 文件里只会有一个值(其他全都会消失)
load 加载数据
input(,'s') 输入字符串类型
whos a 判断a的类型
zeros(n,m) 生成全是0的表格
ones(n,m) 生成全是1的 表格
str2num(s) 把s变为整数/ 计算字符串表达式的结果如“1+1”
tic toc 搭配使用计时
char(c) 把字符串c转换成字符数组/把ASCII码转换成字符
double(c) 把字符c转换ASCII码
isspace(s) s中为空格的返回1,不是空格返回0
find(s,n) 返回s矩阵中(前n个)非零的元素下标
max([]) 返回矩阵最大值和下标
Basic math calculations:
• Defining a function: f=@(x) x^2-1
• Integral: syms z; int(z^2+1), int(z^2+1,0,1) (need tool box)
• Differentiation: syms t; diff(sin(t^2)) (差分数组)
• Limit: limit(sin(t)/t,0)
• Finding a root of a continuous function: fzero(f,0.5) from 0.5 to find(变号根)
• Square root: sqrt(9)
• Nth root: nthroot(4, 3)
The save and load functions:
• Save variables: save('file','var1','var2',...,'format')
• Load variables: load('file','format')
Random number generation:
• An n×m matrix of random numbers: rand(n,m)
• An n×n matrix of random integers between m and M:randi([m M],n)
• A random permutation: randperm(n)
随机一个[1,n]数 randperm(n,1)/ randi(n) / randi([1,n])
循环写法
while expression
statements;
end
for i=1:2:10
statements;
end
break
continue说明速度
a=zeros(1,100000000);
i=1;
tic; while i<=100000000; a(i)=2*(i-1); i=i+1; end; toc;
a=zeros(1,100000000);
tic; for i=1:100000000; a(i)=2*(i-1); end; toc;
tic; [0:2:199999999]; toc;如何写function?
学习博客
细节区分
& 和 && 区别
单引号和双引号的区别
1+1=2;
1+'1'=50
'1'+'1'=98
'1'+"1"="11"
"1"+"1"="11"有意义的代码
A=magic(5); B=A >10;A(B)=0
a=input('Vector: ')
b=(mod(a,2)==0);
c=a.^2;
c(~b)=a(~b).^3

