四则运算
加减乘除 *、-、*、/
指数运算^
octave:2> 2^6 ans = 64
判断是否相等
octave:4> 1~=2 %true ans = 1 octave:3> 1==2 ans = 0
这里的%表示注释
逻辑运算
octave:5> 1&&0 %AND ans = 0 octave:6> 1||0 %OR ans = 1 octave:7> xor(1,0) ans = 1
更换Octave提示符
PS1('>> ')
将提示符更换为>>
Octave变量
octave:2> a = 3 a = 3 octave:3> a = 3; %semiclon suptrssing output(阻止回显)
octave:4> b = 'sd'
b = sd
octave:5> c = (4>=9)
c = 0
octave:6> a = pi
a = 3.1416
octave:7> disp(a)
3.1416
octave:8> disp(sprintf('2 decimals: %.2f',a)) %指定位数输出
2 decimals: 3.14
octave:9> a
a = 3.1416
octave:10> format long
octave:11> a
a = 3.14159265358979
octave:12> format short
octave:13> a
a = 3.1416
向量和矩阵
octave:14> A = [1,2;3,4;5,6] A = 1 2 3 4 5 6 octave:15> v = [1,2,3,4] v = 1 2 3 4 octave:16> v' ans = 1 2 3 4
v = 1:0.1:2 表示1开始 2结束 0.1步长
octave:17> v = 1:0.1:2 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:18> ones(2,3) ans = 1 1 1 1 1 1 octave:19> 2*ones(2,3) ans = 2 2 2 2 2 2
创建一个矩阵,并全置0
octave:20> w = zeros(1,3) w = 0 0 0
获得随机值矩阵
octave:21> rand(2,3) ans = 0.13781 0.70669 0.41409 0.98204 0.64831 0.82677 octave:22> rand(2,3) ans = 0.4731612 0.0042818 0.2550992 0.3454177 0.7322632 0.8613068 octave:23> rand(2,3) ans = 0.8460147 0.9624619 0.9234967 0.0807480 0.0086876 0.7069890
符合高斯分布的随机矩阵
均值为0,标准差为1
octave:24> randn(1,3) ans = -1.64125 1.38290 0.31006 octave:25> randn(2,3) ans = 0.834206 1.829901 -1.775667 1.145843 -0.785595 0.091716
生成矩阵绘制条形图
octave:26> w = -6 + sqrt(10)*randn(1,10000); octave:27> hist(w)
生成单位矩阵
octave:28> eye(4) ans = Diagonal Matrix 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1
help eye查询eye函数的用法,q退出