1. 算术运算
octave:1> 3+2 %加法(plus)
ans = 5
octave:2> 3-12 %减法(minus)
ans = -9
octave:3> 3*4 %乘法(times)
ans = 12
octave:4> 4/2 %除法(divide)
ans = 2
octave:5> 2^4 %幂运算 (power)
ans = 16
octave:6>
2.比较运算
octave:6> 3 == 2 %等值判断
ans = 0
octave:7> 3 ~= 2 %不等判断
3. 逻辑运算
octave:8> 1 && 0 %逻辑与
ans = 0
octave:9> 1 || 0 % %逻辑或
ans = 1
octave:10> xor(1,0) % 异或
ans = 1
4. 修改信息
octave:11> PS1('>>')
>>
5. 变量
>>a = 3
a = 3
>>b = 4; % semicolon supressing output
>>c ='hello';
6. 打印
>> a=pi;
>>a % 方式一
a = 3.1416
>>disp(a) % 方式二
3.1416
>>disp(sprintf('6 decimals: %0.6f',a)) % 方式三
6 decimals: 3.141593
>>format long
>>a
a = 3.141592653589793
>>format short
>>a
a = 3.1416
7. 矩阵
>>A = [1 2; 3 4; 5 6] %三行二列矩阵
A =
1 2
3 4
5 6
>>A = [ 1 2; %三行二列矩阵
> 3 4;
> 5 6]
A =
1 2
3 4
5 6
>>ones(2,3) % 生成2行3列值为1的矩阵
ans =
1 1 1
1 1 1
>>2*ones(2,3)
ans =
2 2 2
2 2 2
>>w = zeros(1,3)
w =
0 0 0
>>rand(2,3) %随机生成0-1之间的矩阵
ans =
0.44691 0.67337 0.34029
0.86973 0.93235 0.49509
>>randn(1,3) %随机生成符合高斯函数分别的矩阵。也就是函数均值为0的矩阵 ans = -0.94615 0.55266 1.67390
>>I = eye(4) % 4*4单位矩阵
I =
Diagonal Matrix
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
8. 向量
>>v = [1;2;3] v = 1 2 3
>>v = 1:0.1:2 % 起始值:步长:结束值
v =
1.0000 1.1000 1.2000 1.3000 1.4000 1.5000 1.6000 1.7000 1.8000 1.9000 2.0000
>>v = 1:6 % 起始值:结束值 默认步长1
v =
1 2 3 4 5 6
9.直方图
>>w = -6 + sqrt(10)*randn(1,10000);
>>hist(w)
10.查看变量
who % 显示所有变量名
Variables in the current scope:
A I ans v w
whos % 可以更详细的显示变量
Variables in the current scope:
Attr Name Size Bytes Class
==== ==== ==== ===== =====
A 3x2 48 double
I 4x4 32 double
ans 1x1 8 double
ex1data1 97x2 1552 double
v 4x1 32 double
w 1x10000 80000 double
Total is 10221 elements using 81672 bytes
11.删除变量
>> clear A %clear 删除所有的变量
12. 帮助
>>help hist