Octave基本语法

基本运算

octave:3> 5+6
ans =  11
octave:4> 3-2
ans =  1
octave:5> 8*9
ans =  72
octave:6> 8/4
ans =  2
octave:7> 6/4
ans =  1.5000
octave:8> 2^6
ans =  64
octave:9>

 

注释:% 表示注释符号

octave:9> 1 == 2 %false
ans = 0
octave:10> 1~=2
ans = 1
octave:11> 1 && 0 % AND
ans = 0
octave:12> 1 || 0 % OR
ans = 1
octave:13> xor(1,0) % 异或
ans = 1
octave:14>

 改变提示符: octave:14> PS1('>> '); ,' ' 内表示所选提示符

 

>> a=3
a =  3
>> a=3;%加上分号将不会显示
>> a
a =  3
>> a=3;
>> a=2
a =  2
>> a=2;%加上分号将不会显示
>> b='hi'
b
= hi
>> c=(3>=1)
c
= 1
>> a=pi
a = 3.1416
>> disp(a)
3.1416
>> disp(sprintf('2 decimals: %0.2f',a)) % 类似于C语言的输出语句
2 decimals: 3.14

 

 

矩阵和向量

>> 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

>> V=[1 2 3]
V =

   1   2   3

>> V=[1;2;3]
V =

   1
   2
   3

>> V=1:0.1:2
V =

 Columns 1 through 7:

    1.0000    1.1000    1.2000    1.3000    1.4000    1.5000    1.6000

 Columns 8 through 11:

    1.7000    1.8000    1.9000    2.0000

>> V=1:6
V =

   1   2   3   4   5   6

>> ones(2,3)
ans =

   1   1   1
   1   1   1

>> 2*ones(2,3)
ans =

   2   2   2
   2   2   2

>> C=[2 2 2;2 2 2]
C =

   2   2   2
   2   2   2

>> w=ones(1,3)
w =

   1   1   1

>> w=zeros(1,3)
w =

   0   0   0

>> w=rand(1,3)
w =

   0.76244   0.97843   0.82005

>> w=rand(3,3) %产生随机数矩阵,范围0~1
w =

   0.844846   0.070033   0.276175
   0.330128   0.777480   0.369802
   0.015571   0.482700   0.163547

>> w = randn(1,3) %产生服从高斯分量的随机矩阵
w =

   0.63218   0.16167  -0.34970

>>
View Code

 

 绘制直方图:

>> w=-6 + sqrt(10)*(randn(1,10000));
>> hist(w)

 

绘制更多竖条的直方图(50个):

>> hist(w,50)

 

 

单位矩阵:

>> i=eye(4)
i =

Diagonal Matrix

   1   0   0   0
   0   1   0   0
   0   0   1   0
   0   0   0   1

 

可输入help命令获得帮助,如:

help eye 

help rand

 

posted @ 2018-10-15 21:29  我星伤悲  阅读(404)  评论(0编辑  收藏  举报