MATLAB
matrix laboratory 计算工具
log 以自然对数为底 log2 底数 只能2和10 其他的用换底公式
variable 都是内存的一块空间
x = 10 meet x + 1 = 2 (false) "=" → 赋值(会覆盖值) terminal运行类似Linux
向量 vector
x = [1,2,3] x = [1 2 3] “;”来分行
A.*B →对应位置相乘
交互式运行脚本
1 2 3 4 5 6 7 8 9 10 11 12 | Input : x = input('Please input x:'); Output : fprintf('output is %f\n',x) ------------if选择---------------- num = input('please input a num'); if num > 22 fprintf('negative'); elseif ..... else fprintf('positive'); end |
disp
1 2 | disp(a);%自动加回车 比较简单的方法 disp(['apple','banana','orange']); |
LOOP
1 2 3 | while xxx ...mod --> 取余 end |
1 2 3 4 5 6 7 8 9 10 11 12 | for i = 10 : -1 : 5 % begin number : decline number : end number disp(i); end v = [5 7 9 10 13 3 2 1] for i = v disp(i); end %内置的sum函数 sum(v); |
function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | %n - input parameter / arguments function mysum(n) sum = 0; for i = 1 : n sum = sum + i; end disp(sum); end ---------------------------------- function result = mysum(a,b) %output - result sum = 0; for i = a : b sum = sum + i; end result = sum; end |
factor 约数
1 2 3 4 5 6 7 8 9 10 | function result = count_facor(n) count = 0; for i = 1 : n if mod(n,i) == 0 count = count + 1; end end result = count; end |
is_primer
1 2 3 4 5 6 7 8 9 | function result = is_primer(n) count = mysum(n); if count == 2 result = 1; else result = 0; end end |
画图

1 2 3 4 | >> x = -3 : 0.1 : 3; >> y = x .^2 ; >> plot(x,y) >> |
1 | >> plot(x,y,'green-o',x,y1,'red-*') |

1 | >> axis equal |
1 2 3 | x = [1 2 3];
y = [10 20 30];
bar(y)
|

1 2 3 4 | grid on; %表示加网格 title(' xxxxx'); xlabel('xxxx'); ylabel(' xxxxx'); |

1 | subplot(record,field,number_setted); |

stem → 用法和plot一样但是是火柴梗图
surf & meshgrid
1 2 3 4 5 | x = -3 : 3; y = -3 : 3; [x,y] = meshgrid(x,y); z = x .^2 + y .^ 2; surf(x,y,z) |

active
1 2 3 4 5 6 7 8 9 10 11 | x = -2 * pi : 0.1 : 2 * pi; y = sin(x); h = plot(x,y); while true : x = x + 0.1; y = sin(x); set(h, 'XData',x,'YData',y); drawnow; end |
ones函数
ones(N) - 生成N行N列且所有元素均为1的矩阵
卷积 → conv
1 2 3 | nx=0:10; x=0.5.^nx; %x[n]用有限长近似 nh=-1:4; h=ones(1,length(nh)); y=conv(x,h); stem([min(nx)+min(nh):max(nx)+max(nh)],y) |
对于诸如spr,exp,log,sin,cos等,内部不能用点乘