octave是Stanford ML著名公开课指定的软件。
其语法特性和MATLAB几乎一样。
- Basic operations
- elementary operation/math operation
+ - * / ^
== ~= && || ~ xor
缩进 PS1('>> ')
这个缩进是octave独有的。
- variable
定义变量a=3; %semicolon supressing output
打印变量disp(a);格式化输出sprintf();字符串用单引号
- ‘全局格式:format long/format short’。例:
a=pi;disp(a);
- 矩阵
- 矩阵和向量的定义
例: A=[1 2;3 4;5 6]或者
B=[1 2;
3 4;
5 6]
row vector: v=[1 2 3]
column vector: c=[1;2;3]
向量生成式: v=start:step:end; 生成的是row vector,想要转换成column vector转置即可。 - 特殊矩阵的定义
- 单位矩阵ones(row,column)
- 零矩阵zeros(row,column)
- 随机矩阵rand(row,column)//高斯随机矩阵randn
- 直方图hist. 例子:w=-6+sqrt(10)*randn(randn(1,10000));hist(w);
- 单位矩阵eye
- 矩阵和向量的定义
- help
例子:help(‘randn’);
- elementary operation/math operation
- Move data around
- 矩阵的大小&向量的大小
sizeof(Matrix)
sizeof(Matrix,1)
sizeof(Matrix,2)
length(v) - 加载数据
load(’filename.dat’) - 查看变量
who - 清除变量
clear - 存储变量
save filename.mat variable; - 读取矩阵中数据
- 访问元素Matrix(1,1)
- 访问某行Matrix(1,:)
- 访问某列Matrix(:,1)
- Matrix to Vector Matrix(:)
- 矩阵的拼接
C=[A B] or C=[A;B]
- 矩阵的大小&向量的大小
- computing on Data
- dot multiply(.*)
- .^
- ./
- log
- exp
- abs
- ‘
- <
- [val ind]=max(vector/matrix)
- magic(num)
- find
- sum sum(A,1) or sum(A,2)
- prod
- floor
- ceil
- max(rand(3),rand(3))
- max(max(A)) or max(A(:))
- flipud
- pinv(A)
Ploting the Data
t=0:0.01:0.98; y1=sin(2*pi*4*t); plot(t,y1); y2=cos(2*pi*4*t); plot(t,y2); plot(t,y1); hold on; plot(t,y2,'r'); xlabel('time'); ylabel('value'); legend('sin','cos'); title('my plot'); print -dpng 'myPlot.png' close; figure(1);plot(t,y1); figure(2);plot(t,y2); subplot(1,2,1);plot(t,y1); subplot(1,2,2);plot(t,y2); axis([0.5 1 -1 1]); clf; A=magic(5); imagesc(A), colorbar, colormap gray; //colon can make user input multiple commands in one line
- control statement (for while if define function)