Octave 是一个与MATLAB语法兼容的科学计算编程语言,在Coursera的 Andrew Ng 《Machine Learning》课程中也是实验推荐的语言,可以用于快速的实现机器学习算法原型。本文是该课程的一节对应的Octave入门教程。
基本操作(Basic Operations)
基本算术运算:加,减法,乘,除,指数运算。
octave:1> 3 + 7
ans = 10
octave:2> 9 - 2
ans = 7
octave:3> 5 * 6
ans = 30
octave:4> 1 / 5
ans = 0.20000
octave:5> 2 ^ 10
ans = 1024
octave:6>
逻辑运算:相等性判断,与,或,非,异或。
octave:6> 1 == 2
ans = 0
octave:7> 1 == 1
ans = 1
octave:8> 1 ~= 2
ans = 1
octave:9> 1 && 2
ans = 1
octave:10> 1 || 0
ans = 1
octave:11> 1 && 0 % AND
ans = 0
octave:14> ~0
ans = 1
octave:15> ~(1 == 1)
ans = 0
octave:12> xor(1, 0)
ans = 1
octave:13> xor(10, 10)
ans = 0
可以修改Octave命令行提示符(Prompt String)。
octave:16> PS1('>> ')
>> 1 + 4
ans = 5
变量(动态语言,没有类型):变量赋值,展示(disp),格式化字符串(sprintf,参照C语言)。format可以改变disp的显示方式。参考文档 Terminal Output
>> a = pi
a = 3.1416
>> a = pi;
>> a
a = 3.1416
>> disp(a);
3.1416
>> disp(sprintf("hello %0.2f", a))
hello 3.14
>> disp(sprintf("hello %0.6f", a))
hello 3.141593
>> format long
>> a
a = 3.141592653589793
>> format short
>> a
a = 3.1416
矩阵,向量:矩阵的表示,矩阵的生成,randn生成服从高斯分布的变量,hist画直方图,eye生成单位矩阵。
>> A = [1 2; 3 4; 5 6]
A =
1 2 3 4 5 6
>> V = [1 2 3]
V =
1 2 3
>> V = [1;2;3]
V =
1 2 3
>> rand(2,3)
ans =
0.65885 0.20695 0.13668 0.71842 0.53510 0.23619
>> randn(2,3)
ans =
0.89741 0.54653 -0.66030 0.41129 -1.61810 -0.69729>> W = -6 + sqrt(10) * (randn(1, 10000));
>> hist(W)
>> eye(3, 3)
ans =
Diagonal Matrix
1 0 0 0 1 0 0 0 1
其中 hist(W)生成的直方图为:
移动数据(Moving data around)
计算矩阵的维度
>> A = [1 2;3 4;5 6]
A =
1 2 3 4 5 6
>> sz = size(A)
sz =
3 2
>> size(sz)
ans =
1 2
>> size(A,1) % first dimesion
ans = 3>> size(A,2)
ans = 2>> length(A)
ans = 3>> v = [1 2 3 4]
v =
1 2 3 4
>> length(v)
ans = 4
切换工作目录。
>> pwd
ans = C:\Users\vonzh
>> cd D:
>> pwd
ans = D:\
加载文件。
>> load featuresX.dat>> load('featuresX.dat') % or this way>> load priceY.dat
>> size(featuresX)
ans =
47 2>> size(priceY)
ans =
47 1
查看当前变量。
>> who
Variables in the current scope:
A V Y ans priceY v
C W a featuresX sz
>> whos
Variables in the current scope:
Attr Name Size Bytes Class
==== ==== ==== ===== =====
A 3x2 48 double C 2x3 48 double V 3x1 24 double W 1x10000 80000 double Y 1x5 40 double a 1x1 8 double ans 1x2 16 double featuresX 47x2 752 double priceY 47x1 376 double sz 1x2 16 double v 1x4 32 double
Total is 10170 elements using 81360 bytes
清除变量。
>> clear featuresX
>> whos
Variables in the current scope:
Attr Name Size Bytes Class
==== ==== ==== ===== =====
A 3x2 48 double C 2x3 48 double V 3x1 24 double W 1x10000 80000 double Y 1x5 40 double a 1x1 8 double ans 1x2 16 double priceY 47x1 376 double sz 1x2 16 double v 1x4 32 double
Total is 10076 elements using 80608 bytes
序列化数据, 默认是二进制格式,可以通过选项指定为文本格式。
>> V = priceY(1:10)V =
399900
329900
369000
232000
539900
299900
314900
198999
212000
242500
>> save hello.mat V;>> clear>> whos>> load hello.mat>> VV =
399900
329900
369000
232000
539900
299900
314900
198999
212000
242500
>> save hello.txt V -ascii % save as text, rather than binary format
矩阵变换
>> A = [1 2;3 4;5 6]
A =
1 2
3 4
5 6
>> A(3,2)
ans = 6
>> A(2,:)
ans =
3 4
>> A([1 3],:) % everything in row 1, row 3
ans =
1 2
5 6
>> A
A =
1 2
3 4
5 6
>> A(:,2) = [7;8;9]
A =
1 7
3 8
5 9
>> A = [A, [100, 101, 102]]
error: horizontal dimensions mismatch (3x2 vs 1x3)
>> A = [A, [100; 101; 102]]
A =
1 7 100
3 8 101
5 9 102
>> A(:) % to a single vector
ans =
1
3
5
7
8
9
100
101
102
>>
>> A = [1 2;3 4;5 6];
>> B = [11 12;13 14;15 16];
>> A
A =
1 2
3 4
5 6
>> B
B =
11 12
13 14
15 16
>> C = [A B]
C =
1 2 11 12
3 4 13 14
5 6 15 16
>> C = [A;B]
C =
1 2
3 4
5 6
11 12
13 14
15 16
计算(Computing on data)
矩阵乘法。
>> A = [1 2;3 4;5 6;]
A =
1 2 3 4 5 6
>> B = [1 1 ;2 2]
B =
1 1 2 2
>> A * B
ans =
5 5 11 11 17 17
矩阵元素相乘,常量相乘。
>> C = [ 11 12; 13 14;15 16]
C =
11 12 13 14 15 16
>> A .* C
ans =
11 24 39 56 75 96>> A .^ 2ans =
1 4 9 16 25 36
>> 1 ./ A
ans =
1.00000 0.50000 0.33333 0.25000 0.20000 0.16667
>> -A
ans =
-1 -2 -3 -4 -5 -6
>> -1 * A
ans =
-1 -2 -3 -4 -5 -6
矩阵转置.
>> A'
ans =
1 3 5 2 4 6
>> (A')'
ans =
1 2 3 4 5 6
元素级别的逻辑运算
>> a < 10ans =
1 0 1 1
>> find( a < 7)
ans =
1 3
矩阵运算常用函数。
>> log(A) % 基于10的对数ans =
0.00000 0.69315
1.09861 1.38629
1.60944 1.79176
>> exp(A) % 指数运算ans =
2.7183 7.3891
20.0855 54.5982
148.4132 403.4288
>> abs(A) % 绝对值ans =
1 2
3 4
5 6
>> abs([-1;2;-3])
ans =
1
2
3
>> a = [ 1 34 6 8.0]
a =
1 34 6 8
>> val = max(a) % 最大值val = 34
>> [val, ind] = max(a)
val = 34
ind = 2
>> A = magic(3)
A =
8 1 6
3 5 7
4 9 2
>> [r,c] = find(A >= 7) % 查找符合条件的元素index
r =
1
3
2
c =
1
2
3
>> A = [ 1 2 3]
A =
1 2 3
>> sum(A) % 求和ans = 6
>> prod(A)
ans = 6
>> A = [1 2 3 0.5]
A =
1.00000 2.00000 3.00000 0.50000
>> floor(A) % 向下取整ans =
1 2 3 0
>> ceil(A) % 向上取整ans =
1 2 3 1
>> A = [1 2;3 4;5 6]
A =
1 2
3 4
5 6
>> B = [0 1;10 11; 4 9]
B =
0 1
10 11
4 9
>> max(A,B) % 取各个位置的最大值ans =
1 2
10 11
5 9
>> A
A =
1 2
3 4
5 6
>> max(A,[],1) % 各列的最大值ans =
5 6
>> max(A,[],2) % 各行的最大值ans =
2
4
6
>> max(A(:))
ans = 6
A =
47 58 69 80 1 12 23 34 45
57 68 79 9 11 22 33 44 46
67 78 8 10 21 32 43 54 56
77 7 18 20 31 42 53 55 66
6 17 19 30 41 52 63 65 76
16 27 29 40 51 62 64 75 5
26 28 39 50 61 72 74 4 15
36 38 49 60 71 73 3 14 25
37 48 59 70 81 2 13 24 35
>> sum(A,1)
ans =
369 369 369 369 369 369 369 369 369
>> sum(sum(A.*eye(9))) % 求对角线元素之和ans = 369
>> sum(sum(A.*flipud(eye(9))))
ans = 369
>> A = magic(3)
A =
8 1 6
3 5 7
4 9 2
>> temp = pinv(A) % 伪逆矩阵temp =
0.147222 -0.144444 0.063889
-0.061111 0.022222 0.105556
-0.019444 0.188889 -0.102778
>> temp * A
ans =
1.0000e+000 2.0817e-016 -3.1641e-015
-6.1062e-015 1.0000e+000 6.2450e-015
3.0531e-015 4.1633e-017 1.0000e+000
其中, magic 产生魔法矩阵(magic matrix,横纵,对角线之和均相等),注意 2 X 2 的魔法矩阵不存在。
>> magic(2)
ans =
4 3 1 2
数据可视化(Plotting 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 'sincos.png' % 保持图像
>> close % 关闭图像
```
```
>>
>> figure(1);plot(t,y1); % 可以指定多个图像
>> figure(2);plot(t,y2);
可以在同一个图像(figure)上实现多个绘制(plot)。
subplot (rows, cols, index) :Set up a plot grid with rows by cols subwindows and set the current axes for plotting (gca) to the location given by index.
>> subplot(1,2,1);
>> plot(t,y1);
>> subplot(1,2,2);
>> plot(t,y2);
改变横纵坐标的显示范围。
>> axis([0.5 2 -1 2])
矩阵表示为彩色图像形式。
>> clf; % 清空图像>> A = magic(5);
>> A
A =
17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9
>> imagesc(A)
>> imagesc(A), colorbar, colormap gray;
控制语句、函数
for 循环。
>> v=zeros(10,1)v =
0
0
0
0
0
0
0
0
0
0>> for i=1:10,> v(i) = 2 ^ i;> end;>> vv =
2
4
8
16
32
64
128
256
512
1024
>> indices=1:10indices =
1 2 3 4 5 6 7 8 9 10
>> for i=indices,> disp(i);> end; 1
2
3
4
5
6
7
8
9
10
while 循环。
>> i = 1;
>> while i<= 5;
> v(i)=100;
> i=i+1;
> end;
>> v
v =
100 100 100 100 100 64 128 256 512 1024
break 语句。
>> i=1;>> while true,> v(i)=999;> i=i+1;> if i==6,> break;> end;> end;>> vv =
999
999
999
999
999
64
128
256
512
1024
if-else 语句。
>> v(1)=2;
>> if v(1)==1,
> disp('one');
> elseif v(1)==2,
> disp('two');
> else
> disp('others');
> end;
two
函数定义,在工作目录(或者使用addpath加入)新建下面的文件 squareThisNumber.m 。
function y = squareThisNumber(x)
y = x ^ 2;
运行效果:
>> squareThisNumber(5)
ans = 25
Octave 中函数可以返回多个值。
% squareAndCubeThisNumber.m
function [y1, y2] = squareAndCubeThisNumber(x)
y1 = x ^ 2;
y2 = x ^ 3;
运行效果:
>> [a,b]=squareAndCubeThisNumber(5)
a = 25b = 125
函数举例:成本函数J。
>> X = [1 1;1 2;1 3]
X =
1 1 1 2 1 3
>> y = [1; 2; 3]
y =
1 2 3
>> theta = [0;1];
>> J = costFunctionJ(X,y,theta);
>> J
J = 0>> theta = [0;0];
>> J = costFunctionJ(X,y,theta)
J = 2.3333
向量化(Vectorial implementation)
向量化思维,把问题转化为向量运算,利用编程语言提供的库函数,进行高效运算。
参考资料