5.1 基本操作
Octave下载地址:https://www.gnu.org/software/octave/download.html
octave:1> PS1('>>');
>>
PS1('>>'):表示更换命令提示符。其中单引号中的内容可换成自己习惯用的任意符号。
简单语法和python一致,如下:
>>1+1
ans = 2
>>1-1
ans = 0
>>2*2
ans = 4
>>1/2
ans = 0.50000
>>2^2
ans = 4
>>1 == 2 % 判断是否等于
ans = 0
>>1 ~= 2 % 判断是否不等于
ans = 1
>>1 && 0 % 且
ans = 0
>>1 || 0 % 或
ans = 1
不一样的地方在于,在其他语言中不等于号为‘ != ’,在octave中不等于符号为‘ ~= ’。
异或运算:
异或(xor)是一个数学运算符。它应用于逻辑运算。异或的数学符号为“⊕”,计算机符号为“xor”。其运算法则为:
a⊕b = (¬a ∧ b) ∨ (a ∧¬b)
如果a、b两个值不相同,则异或结果为1。如果a、b两个值相同,异或结果为0。
例子:
>>xor(1,0)
ans = 1代码块
其他简单语法如下:
>>a = 3 % 把3赋值给a
a = 3
>>a = 3; % 加分号会阻止输出
>>b = 'abc'; % 字符串赋值
>>b % 输出b
b = abc
>>c = (3>=4); % c赋为假值
>>c
c = 0
>>a = pi; % 圆周率π
>>a
a = 3.1416
>>disp(a); % 特殊输出
3.1416
>>disp(sprintf(' 2 decimals: %0.2f', a)) % 自定义格式输出
2 decimals: 3.14
>>disp(sprintf(' 6 decimals: %0.6f', a)) % 六位小数输出
6 decimals: 3.141593
>>format long % 长型输出
>>a
a = 3.14159265358979
>>format short % 短型输出
>>a
a = 3.1416
向量和矩阵:
>>A = [1 2; 3 4; 5 6] % 矩阵赋值
A =
1 2
3 4
5 6
>>A = [6 5; % 另一种定义矩阵的方式
> 4 3;
> 2 1]
A =
6 5
4 3
2 1
>>V = [1 2 3] % 定义一个行向量或3*1矩阵
V =
1 2 3
>>V = [1; 2; 3] % 定义一个列向量或1*3矩阵
V =
1
2
3
octave:4> V = 1:0.1:2 % 定义一个行向量从1开始到2,间距为0.1(1*11矩阵)
V =
Columns 1 through 8:
1.0000 1.1000 1.2000 1.3000 1.4000 1.5000 1.6000 1.7000
Columns 9 through 11:
1.8000 1.9000 2.0000
octave:1> V = 1:6 % 定义一个行向量或6*1矩阵,octave默认间距为1
V =
1 2 3 4 5 6
octave:2> ones(2,3) % 生成一个2*3矩阵,默认所有元素为1
ans =
1 1 1
1 1 1
octave:3> C = 2*ones(2,3) % 生成一个2*3矩阵,设置所有元素为2
C =
2 2 2
2 2 2
octave:4> C = [2 2 2; 2 2 2]
C =
2 2 2
2 2 2
octave:5> W = zeros(1,3)
W =
0 0 0
octave:6> W = rand(1,3) % 生成随机矩阵或向量(所有项的值介于0-1)
W =
0.24500 0.59770 0.99636
octave:1> rand(3,3) % 生成随机矩阵或向量(所有项的值介于0-1)
ans =
0.74228 0.20606 0.61900
0.33074 0.43885 0.29686
0.89678 0.10433 0.73303
octave:2> W = randn(1,3) % 服从高斯分布(均值为0,标准差或者方差为1)
W =
0.36618 0.67564 -1.44800
>>W = -6 + sqrt(10)*(randn(1,10))
W =
-11.7131 -10.2997 -4.7039 -9.6148 -4.0568 -6.9152 -3.4757 -2.9873 -9.6401 -2.4721
>>hist(W)
>>hist(W,50)
>>
octave:1> eye(4) % 生成4*4的单位矩阵
ans =
Diagonal Matrix
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
octave:2> I =eye(4)
I =
Diagonal Matrix
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
octave:5> help rand % 显示rand方法的帮助函数
'rand' is a built-in function from the file libinterp/corefcn/rand.cc
-- rand (N)
-- rand (M, N, ...)
-- rand ([M N ...])
-- V = rand ("state")
-- rand ("state", V)
-- rand ("state", "reset")
-- V = rand ("seed")
-- rand ("seed", V)
-- rand ("seed", "reset")
-- rand (..., "single")
-- rand (..., "double")
Return a matrix with random elements uniformly distributed on the
interval (0, 1).
The arguments are handled the same as the arguments for 'eye'.
You can query the state of the random number generator using the
form
v = rand ("state")
This returns a column vector V of length 625. Later, you can
restore the random number generator to the state V using the form
rand ("state", v)
You may also initialize the state vector from an arbitrary vector
of length <= 625 for V. This new state will be a hash based on the
value of V, not V itself.
By default, the generator is initialized from '/dev/urandom' if it
is available, otherwise from CPU time, wall clock time, and the
current fraction of a second. Note that this differs from MATLAB,
which always initializes the state to the same state at startup.
To obtain behavior comparable to MATLAB, initialize with a
deterministic state vector in Octave's startup files (*note Startup
Files::).
To compute the pseudo-random sequence, 'rand' uses the Mersenne
Twister with a period of 2^{19937}-1 (See M. Matsumoto and T.
Nishimura, 'Mersenne Twister: A 623-dimensionally equidistributed
uniform pseudorandom number generator', ACM Trans. on Modeling and
Computer Simulation Vol. 8, No. 1, pp. 3-30, January 1998,
). Do
*not* use for cryptography without securely hashing several
returned values together, otherwise the generator state can be
learned after reading 624 consecutive values.
Older versions of Octave used a different random number generator.
The new generator is used by default as it is significantly faster
than the old generator, and produces random numbers with a
significantly longer cycle time. However, in some circumstances it
might be desirable to obtain the same random sequences as produced
by the old generators. To do this the keyword "seed" is used to
specify that the old generators should be used, as in
rand ("seed", val)
which sets the seed of the generator to VAL. The seed of the
generator can be queried with
s = rand ("seed")
However, it should be noted that querying the seed will not cause
'rand' to use the old generators, only setting the seed will. To
cause 'rand' to once again use the new generators, the keyword
"state" should be used to reset the state of the 'rand'.
The state or seed of the generator can be reset to a new random
value using the "reset" keyword.
The class of the value returned can be controlled by a trailing
"double" or "single" argument. These are the only valid classes.
See also: randn, rande, randg, randp.
Additional help for built-in functions and operators is
available in the online version of the manual. Use the command
'doc ' to search the manual index.
Help and information about Octave is also available on the WWW
at https://www.octave.org and via the [email protected]
mailing list.
>>
5.2 移动数据和数据的计算
如果你有一个机器学习问题,你怎样把数据加载到 Octave 中?
举例:
>>A = [1 2; 3 4; 5 6]
A =
1 2
3 4
5 6
>>size(A) % size() 命令返回矩阵的尺寸
ans =
3 2
>>sz = size(A) % 命令返回的是一个 1×2 的矩阵,我们可以用 来存放。
sz =
3 2
>>size(sz)
ans =
1 2
>>size(A, 1) % 这个命令会返回A矩阵的第一个维度的尺寸,也就是A矩阵的行数。
ans = 3
>>size(A, 2) % 这个命令会返回A矩阵的第二个维度的尺寸,也就是A矩阵的列数。
ans = 2
>>v = [1 2 3 4]
v =
1 2 3 4
>>length(v) % 这个命令将返回最大维度的大小。
ans = 4
>>length(A)
ans = 3
>>length([1;2;3;4;5]) % 也会求向量的大小。
ans = 5
>>pwd % 显示当前目录
ans = C:\Users\93780
>>ls % 显示当前目录所有文件
驱动器 C 中的卷是 Windows
卷的序列号是 EED5-9FEB
C:\Users\93780 的目录
[.] [3D Objects]
[..] [Cisco Packet Tracer 6.2sv]
[.android] [Contacts]
[.AndroidStudio2.3] [Desktop]
.bash_history [Documents]
[.config] [Downloads]
[.dotnet] [Evernote]
[.eclipse] [fancy]
.gitconfig [Favorites]
[.gradle] [iNodeClient]
[.IdeaIC2017.2] [Links]
[.LSC] [Music]
[.nbi] [OneDrive]
.octave_hist [Pictures]
[.Origin] [Roaming]
[.p2] [Saved Games]
.packettracer [Searches]
[.QtWebEngineProcess] [source]
[.ssh] [Videos]
[.tooling]
[.VirtualBox]
4 个文件 4,008 字节
36 个目录 86,981,881,856 可用字节
>>cd 'c:\jiqi\' % 去往其他地址的文件夹
>>pwd
ans = c:\jiqi
>>ls
驱动器 C 中的卷是 Windows
卷的序列号是 EED5-9FEB
c:\jiqi 的目录 %文件夹内有featuresX.dat 和priceY.dat两个数据文件
[.] [..] featuresX.dat priceY.dat
2 个文件 25,136 字节
2 个目录 86,194,446,336 可用字节
>>load featuresX.dat %打开数据文件
>>load priceY.dat
>>featuresX %输出数据文件内容
featuresX =
2104 3
1600 3
2400 3
1416 2
3000 4
1985 4
1534 3
1427 3
1380 3
1494 3
1940 4
2000 3
1890 3
4478 5
1268 3
1437 3
1239 3
2132 4
4215 4
2162 4
1664 2
2238 3
2567 4
1200 3
852 2
1852 4
1203 3
>>who %显示出工作空间中的所有变量
Variables in the current scope:
featuresX priceY
>>whos %更详细的显示出工作空间中的所有变量
Variables in the current scope:
Attr Name Size Bytes Class
==== ==== ==== ===== =====
ans 1x2 16 double
featuresX 27x2 432 double
priceY 10x1 80 double
Total is 66 elements using 528 bytes
>>size(featuresX) %代表这是一个27×2的矩阵
ans =
27 2
>>clear featuresX %删除变量
>>who %查询后可以看出删除了变量
Variables in the current scope:
ans priceY
>>V=priceY(1:10) %将priceY向量的前10个元素存入V中
V =
3999
3299
3690
2320
5399
2999
3149
1989
2120
2425
>>save hello.mat V %这个命令会将变量存成一个叫 hello.mat 的文件
>>clear %删除工作空间中的所有变量
>>who %没有发现任何变量
>>whos
>>load hello.mat %载入刚才保存的文件
>>V
V =
3999
3299
3690
2320
5399
2999
3149
1989
2120
2425
>>who %我们能发现在载入的时候也会把变量恢复
Variables in the current scope:
V
>>save hello.txt V -ascii %将数据的 ascii 码存成文本文档。
>>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(:,2) %取得第二列的值
ans =
2
4
6
>>A([1 3],:) %取得第一三行的值
ans =
1 2
5 6
>>A(:,2)=[10;11;12] %更新第二列的值
A =
1 10
3 11
5 12
>>A=[A,[100;101;102]] %加入第三列的值
A =
1 10 100
3 11 101
5 12 102
>>A(:) %把矩阵的所有值以向量的方式表达出来
ans =
1
3
5
10
11
12
100
101
102
>>A %但是A(:)并不会改变A的储存方式
A =
1 10 100
3 11 101
5 12 102
>>A=[1 2;3 4;5 6]
A =
1 2
3 4
5 6
>>B=[11 12; 13 14; 15 16]
B =
11 12
13 14
15 16
>>C = [A B] %A与B以列相加的方式合成C
C =
1 2 11 12
3 4 13 14
5 6 15 16
>>C = [A; B] %A与B以行相加的方式合成C
C =
1 2
3 4
5 6
11 12
13 14
15 16
>>A
A =
1 2
3 4
5 6
>>B
B =
11 12
13 14
15 16
>>C=[1 1;2 2]
C =
1 1
2 2
>>A*C
ans =
5 5
11 11
17 17
>>A.*B %.是元素位运算,结果是两个矩阵的对应元素相乘
ans =
11 24
39 56
75 96
>>A.^2 %.是元素位运算,结果是矩阵每个元素的平方
ans =
1 4
9 16
25 36
>>V=[1; 2; 3]
V =
1
2
3
>>1./V %取其倒数
ans =
1.00000
0.50000
0.33333
>>1./A
ans =
1.00000 0.50000
0.33333 0.25000
0.20000 0.16667
>>log(V) %取其对数
ans =
0.00000
0.69315
1.09861
>>exp(V) %取其e的V次幂
ans =
2.7183
7.3891
20.0855
>>-V %取其负数
ans =
-1
-2
-3
>>abs(-V) %取其绝对值
ans =
1
2
3
>>ones(length(V) ,1) %取其加1的值,length(V)取V长度,ones()构建矩阵。
ans =
1
1
1
>>V+ones(length(V) ,1)
ans =
2
3
4
>>V+1 %每一项加1
ans =
2
3
4
>>A' %取其转置
ans =
1 3 5
2 4 6
>>(A')' %取其转置的转置
ans =
1 2
3 4
5 6
>>a=[1 15 2 0.5]
a =
1.00000 15.00000 2.00000 0.50000
>>val=max(a) %取其的最大值
val = 15
>>[val, ind] =max(a) %取其的最大值与最小值
val = 15
ind = 2
>>max(A) %取其矩阵每一列的最大值
ans =
5 6
>>a<3 %判断其大于3的元素
ans =
1 0 1 1
>>find(a<3) %取其大于3的元素
ans =
1 3 4
>>A = magic(3) %magic()将返回一个矩阵,称为魔方阵或幻方 ,它们具有所有的行和列和对角线加起来都等于相同的值的性质。
A =
8 1 6
3 5 7
4 9 2
>>[r,c] = find(A>=7) %将找出所有矩阵中大于等于7的元素,因此, 和分别表示行和列,这就表示,第一行第一列的元素大于等于7,第三行第二列的元素大于等于7,第二行第三列的元素大于等于7。
r =
1
3
2
c =
1
2
3
>>a
a =
1.00000 15.00000 2.00000 0.50000
>>sum(a) %求a中所有元素的和
ans = 18.500
>>prod(a) %求a中所有元素的乘积
ans = 15
>>floor(a) %向下四舍五入
ans =
1 15 2 0
>>ceil(a) %向上四舍五入
ans =
1 15 2 1
>>max(rand(3),rand(3)) %返回两个3×3的随机矩阵,并且逐元素比较取最大值。
ans =
0.43983 0.91049 0.55051
0.78066 0.37371 0.17633
0.87434 0.99893 0.80515
>>max(A,[],1) %得到每一列的最大值
ans =
8 9 7
>>max(A,[],2) %得到每一行的最大值
ans =
8
7
9
>>max(max(A)) %找出整个矩阵A的最大值
ans = 9
>>max(A(:)) %找出整个矩阵A的最大值
ans = 9
>>A=magic(9)
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(A,2) %得到每一行的总和
ans =
369
369
369
369
369
369
369
369
369
>>a=[3,4;2,16]
a =
3 4
2 16
>>pinv(a) %得到其逆矩阵
ans =
0.400000 -0.100000
-0.050000 0.075000
>>pinv(a)*a %其逆矩阵与其本身相乘
ans =
1.00000 -0.00000
0.00000 1.00000
>>a
5.3 图形化表示
>>t=[0:0.01:0.98];
>>t
t =
Columns 1 through 8:
0.00000 0.01000 0.02000 0.03000 0.04000 0.05000 0.06000 0.07000
Columns 9 through 16:
0.08000 0.09000 0.10000 0.11000 0.12000 0.13000 0.14000 0.15000
Columns 17 through 24:
0.16000 0.17000 0.18000 0.19000 0.20000 0.21000 0.22000 0.23000
Columns 25 through 32:
0.24000 0.25000 0.26000 0.27000 0.28000 0.29000 0.30000 0.31000
Columns 33 through 40:
0.32000 0.33000 0.34000 0.35000 0.36000 0.37000 0.38000 0.39000
Columns 41 through 48:
0.40000 0.41000 0.42000 0.43000 0.44000 0.45000 0.46000 0.47000
Columns 49 through 56:
0.48000 0.49000 0.50000 0.51000 0.52000 0.53000 0.54000 0.55000
Columns 57 through 64:
0.56000 0.57000 0.58000 0.59000 0.60000 0.61000 0.62000 0.63000
Columns 65 through 72:
0.64000 0.65000 0.66000 0.67000 0.68000 0.69000 0.70000 0.71000
Columns 73 through 80:
0.72000 0.73000 0.74000 0.75000 0.76000 0.77000 0.78000 0.79000
Columns 81 through 88:
0.80000 0.81000 0.82000 0.83000 0.84000 0.85000 0.86000 0.87000
Columns 89 through 96:
0.88000 0.89000 0.90000 0.91000 0.92000 0.93000 0.94000 0.95000
Columns 97 through 99:
0.96000 0.97000 0.98000
>>y1=sin(2*pi*4*t); %正弦函数
>>plot(t,y1)
%横轴是t变量,纵轴是y1。
>>y2=cos(2*pi*4*t);
>>plot(t,y2)
>>plot(t,y1)
>>hold on; %在已经生成的图像中画新的图
>>plot(t,y2,'r')
>>xlabel('time')%横轴与纵轴单位
>>xlabel('value')
>>legend('sin','cos')%右上角做表示
>>title('plot')%标题
>>cd 'C:\jiqi'; print -dpng 'Plot.png'%生成图片
>>close
>>figure(2); plot(t,y2);
>>figure(1); plot(t,y1);
>>subplot(1,2,1) %使用图像中第一块空间
>>plot(t,y1); %第一块空间写入此图像
>>subplot(1,2,2)
>>plot(t,y2);
>>axis([0.5 1 -1 1]) %设置数值
>>clf %清除图像
>>A = magic(5)
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;
5.4 控制语句
>>v=zeros(10,1)
v =
0
0
0
0
0
0
0
0
0
0
>>for i=1:10,
> v(i)=2^i;
> end;
>>v
v =
2
4
8
16
32
64
128
256
512
1024
>>indices=1:10;
>>indices
indices =
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
>>i = 1;
>>while i <= 5,
> v(i) = 100;
> i = i+1;
> end;
>>v
v =
100
100
100
100
100
64
128
256
512
1024
>>i=1;
>>while true,
> v(i) = 999;
> i=i+1;
> if i == 6,
> break;
> end;
> end;
>>v
v =
999
999
999
999
999
64
128
256
512
1024
>>v(1)=2;
>>if v(1)==1,
> disp('v(1)=1');
> elseif v(1)==2,
> disp('v(1)=2');
> else
> disp('v(1)=1 or v(1)=2');
> end;
v(1)=2
>>end;
>>exit%退出Octave
>>quit%退出Octave
5.5 如何定义和调用函数
>>cd 'C:\jiqi' %进入函数文件即.m文件的目录内
>>squareThisNumber(5) %使用函数即.m文件
ans = 25
>>addpath('C:\jiqi') %把此目录设置成环境路径之一
>>cd 'C:\'
>>pwd
ans = C:\
>>squareThisNumber(5)
ans = 25
>>[a,b] = squareAndCubeThisNumber(5); %取出输出的值
>>a
a = 25
>>b
b = 125
squareThisNumber.m文件内代码如下:
function y = squareThisNumber(x)
y = x^2;
%这就告诉 Octave,我想返回一个y值,我想返回一个值,并且返回的这个值将被存放于变量y里。另外,它告诉了Octave这个函数有一个参数,就是参数x,还有定义的函数体,也就是y等于x的平方。
squareAndCubeThisNumber.m文件内代码如下:
function [y1,y2] = squareAndCubeThisNumber(x)
y1 = x^2;
y2 = x^3;
%这就告诉 Octave,我想返回两个y值。
以下用一个完整的例子来总结上面的知识