Octave教程

Windows安装Octave

http://wiki.octave.org/Octave_for_Microsoft_Windows

基本操作(Basic Operations)

octave:1> PS1('>> ');
>>
>> path
>> pause
>> clear ; close all; clc
>> fprintf(['test print %%s : %s \n' ...
         'test print %%d : %d \n' ...
         'test print %%g : %g \n' ...
         'test print %%f : %f \n' ...  
        ], 'String', 10, 2.33553e-11, 0.12345678);
test print %s : String 
test print %d : 10 
test print %g : 2.33553e-11 
test print %f : 0.123457 
>>
>> 1+2
ans =  3
>>  5*8
ans =  40
>>  1/2
ans =  0.50000
>>  2^6
ans =  64
>>  2^3
ans =  8
>>  1 == 2 % false
ans = 0
>>  1 == 1 % true
ans = 1
>>  1 ~= 2 % true
ans = 1
>>  1 ~= 2 % true
ans = 1
>>  1 ~= 1 % false
ans = 0
>>  1 ~= 1 % false  not equal
ans = 0
>>  1 && 0
ans = 0
>>  1 && 0 % AND
ans = 0
>>  1 || 0 % or
ans = 1
>>  xor(1,0)
ans = 1
>>  xor(1,0) % yi or
ans = 1
>> PS1('>> ');
>> a=3
a =  3
>> a=4;
>> a
a =  4
>> c=(3>=4)
c = 0
>> c
c = 0
>> a=pi
a =  3.1416
>> a=pi;
>> a
a =  3.1416
>> b=e;
>> b
b =  2.7183
>> disp(a);
 3.1416
>> disp(sprintf('2 decimals: %0.2f', a))
2 decimals: 3.14
>> disp(sprintf('10 decimals: %0.10f', a))
10 decimals: 3.1415926536
>> disp(sprintf('10 decimals: %0.10f', b))
10 decimals: 2.7182818285
>> format long
>> a
a =  3.141592653589793
>> b
b =  2.718281828459045
>>  format short
>> a
a =  3.1416
>> b
b =  2.7183
>> A = [1 2; 3 4; 5 6];
>>  A
A =

   1   2
   3   4
   5   6

>>  A
A =

   1   2
   3   4
   5   6

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

   1
   2
   3

>>  v=1:0.1:2;
>>  v
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
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

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

   1   1   1

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

   0   0   0

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

   0.066378   0.828597   0.569681

>>  rand(3,3)
ans =

   0.2443954   0.3866398   0.2332407
   0.6611382   0.6353541   0.0066938
   0.2465706   0.2734220   0.6748704

>>  rand(3,3)
ans =

   0.71983   0.47680   0.30126
   0.19673   0.29252   0.57751
   0.99891   0.57380   0.31022

>>  rand(3,3)
ans =

   0.26842   0.82579   0.22678
   0.63709   0.67602   0.11723
   0.83274   0.48216   0.26778

>>  rand(3,3)
ans =

   0.790568   0.666798   0.084361
   0.343994   0.482338   0.860989
   0.908285   0.967817   0.935013

>>  randn(1,3) % gaosi fenbu
ans =

  -0.098394  -0.147581   0.085802

>>  randn(1,3) % gaosi fenbu
ans =

   1.10694  -1.99933  -0.17373

>>  w= -6 + sqrt(10) * (randn(1,10000));
>>  hist(w)
>>  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 eye
>>  help help

移动数据(Moving Data Around)

>>  A = [1 2; 3 4; 5 6]
A =

   1   2
   3   4
   5   6

>>  size(A)
ans =

   3   2

>>  size(A, 1)
ans =  3
>>  size(A, 2)
ans =  2
>>  size(A, 3)
ans =  1
>>  v = [1 2 3 4]
v =

   1   2   3   4

>>  length(v)
ans =  4
>>  length(A)
ans =  3

>> load featuresX.dat
>> load ('priceY.dat')
>> who
Variables in the current scope:

A          ans        featuresX  priceY     v

>> whos
Variables in the current scope:

   Attr Name           Size                     Bytes  Class
   ==== ====           ====                     =====  =====
        A              3x2                         48  double
        ans            1x23                        23  char
        featuresX      3x1                         24  double
        priceY         3x1                         24  double
        v              1x4                         32  double

Total is 39 elements using 151 bytes

>> priceY
priceY =

   1000
   2000
   3000

>> featuresX
featuresX =

   1234
   5678
   1235

>> clear featuresX
>>
>> whos
Variables in the current scope:

   Attr Name        Size                     Bytes  Class
   ==== ====        ====                     =====  =====
        A           3x2                         48  double
        ans         1x23                        23  char
        priceY      3x1                         24  double
        v           1x4                         32  double

Total is 36 elements using 127 bytes

>> clear
>> whos
>> load ('priceY.dat')
>> v = priceY(2:3)
v =

   2000
   3000

>> save hello.mat v;
>> clear
>> whos
>> load hello.mat
>> whos
Variables in the current scope:

   Attr Name        Size                     Bytes  Class
   ==== ====        ====                     =====  =====
        v           2x1                         16  double

Total is 2 elements using 16 bytes

>> v
v =

   2000
   3000

>> save hello.txt -ascii % save as text (ASCII)
>> A = [1 2;3 4;5 6]
A =

   1   2
   3   4
   5   6

>> A(3,2)
ans =  6
>>
>> A(2,:) % ":" means every element along that row/column
ans =

   3   4

>> A(:,2)
ans =

   2
   4
   6

>> A([1 3], :)
ans =

   1   2
   5   6

>> A
A =

   1   2
   3   4
   5   6

>> A([1 2 3], :)
ans =

   1   2
   3   4
   5   6

>> A(:,2) = [10; 20; 30]
A =

    1   10
    3   20
    5   30

>> A = [A, [100; 101; 102]] % append another column vector to right
A =

     1    10   100
     3    20   101
     5    30   102

>> A = [A; [100 101 102]] % append another column vector to right
A =

     1    10   100
     3    20   101
     5    30   102
   100   101   102

>> A(:)
ans =

     1
     3
     5
   100
    10
    20
    30
   101
   100
   101
   102
   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]
C =

    1    2   11   12
    3    4   13   14
    5    6   15   16

>> [B A]
ans =

   11   12    1    2
   13   14    3    4
   15   16    5    6

>> [A B C]
ans =

    1    2   11   12    1    2   11   12
    3    4   13   14    3    4   13   14
    5    6   15   16    5    6   15   16

>> [A; B]
ans =

    1    2
    3    4
    5    6
   11   12
   13   14
   15   16

>> [A, B]
ans =

    1    2   11   12
    3    4   13   14
    5    6   15   16

计算数据(Computing on Data)

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

>> 3 * A
ans =

    3    6
    9   12
   15   18

>> A*3
ans =

    3    6
    9   12
   15   18

>> A
A =

   1   2
   3   4
   5   6

>> B
B =

   11   12
   13   14
   15   16

>> 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)
ans =

    2.7183
    7.3891
   20.0855

>> abs(v)
ans =

   1
   2
   3

>> abs([-1; 2; -3])
ans =

   1
   2
   3

>> -v
ans =

  -1
  -2
  -3

>> -A
ans =

  -1  -2
  -3  -4
  -5  -6

>> -1*v
ans =

  -1
  -2
  -3

>> v+ones(length(v),1)
ans =

   2
   3
   4

>> ones(length(v),1)
ans =

   1
   1
   1

>> ones(length(v),2)
ans =

   1   1
   1   1
   1   1

>> zeros(length(v),2)
ans =

   0   0
   0   0
   0   0

>> v.+1
ans =

   2
   3
   4

>> v.-1
ans =

   0
   1
   2

>> 1.+v
ans =

   2
   3
   4

>> v+1
ans =

   2
   3
   4

>> v-1
ans =

   0
   1
   2

>> A
A =

   1   2
   3   4
   5   6

>> A'
ans =

   1   3   5
   2   4   6

>> A''
ans =

   1   2
   3   4
   5   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
ans =

  1  0  1  1

>> A < 3
ans =

  1  1
  0  0
  0  0

>> find(a<3)
ans =

   1   3   4

>> a
a =

    1.00000   15.00000    2.00000    0.50000

>> magic(3)
ans =

   8   1   6
   3   5   7
   4   9   2

>> magic(4)
ans =

   16    2    3   13
    5   11   10    8
    9    7    6   12
    4   14   15    1

>> magic(3)
ans =

   8   1   6
   3   5   7
   4   9   2

>> magic(3)
ans =

   8   1   6
   3   5   7
   4   9   2

>> magic(3)
ans =

   8   1   6
   3   5   7
   4   9   2

>> magic(3)
ans =

   8   1   6
   3   5   7
   4   9   2

>> [r,c] = find(A >= 7)
r = [](0x1)
c = [](0x1)
>> A
A =

   1   2
   3   4
   5   6

>> A=magic(3)
A =

   8   1   6
   3   5   7
   4   9   2

>> [r,c] = find(A >= 7)
r =

   1
   3
   2

c =

   1
   2
   3

>> [r,c] = find(A >= 2)
r =

   1
   2
   3
   2
   3
   1
   2
   3

c =

   1
   1
   1
   2
   2
   3
   3
   3

>> a
a =

    1.00000   15.00000    2.00000    0.50000

>> sum(a)
ans =  18.500
>> prod(a)
ans =  15
>> floor(a)
ans =

    1   15    2    0

>> ceil(a)
ans =

    1   15    2    1

>> rand(3)
ans =

   0.43298   0.41511   0.89855
   0.21145   0.15275   0.96643
   0.67367   0.31471   0.15306

>> max(rand(3), rand(3))
ans =

   0.99326   0.98554   0.83527
   0.78799   0.71117   0.92512
   0.99326   0.98996   0.34543

>> A
A =

   8   1   6
   3   5   7
   4   9   2

>> max(A, [], 1)
ans =

   8   9   7

>> max(A, [], 2)
ans =

   8
   7
   9

>> max(A)
ans =

   8   9   7

>> max(max(A))
ans =  9
>> A(:)
ans =

   8
   3
   4
   1
   5
   9
   6
   7
   2

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

>> eye(9)
ans =

Diagonal Matrix

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

>> A .* eye(9)
ans =

   47    0    0    0    0    0    0    0    0
    0   68    0    0    0    0    0    0    0
    0    0    8    0    0    0    0    0    0
    0    0    0   20    0    0    0    0    0
    0    0    0    0   41    0    0    0    0
    0    0    0    0    0   62    0    0    0
    0    0    0    0    0    0   74    0    0
    0    0    0    0    0    0    0   14    0
    0    0    0    0    0    0    0    0   35

>> sum(A .* eye(9))
ans =

   47   68    8   20   41   62   74   14   35

>> sum(sum(A .* eye(9)))
ans =  369
>> flipud(eye(9))
ans =

Permutation Matrix

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

>> sum(sum(A .* flipud(eye(9))))
ans =  369
>>
>>
>>
>>
>> A = magic(3)
A =

   8   1   6
   3   5   7
   4   9   2

>> pinv(A) % pseudoinverse
ans =

   0.147222  -0.144444   0.063889
  -0.061111   0.022222   0.105556
  -0.019444   0.188889  -0.102778

>> inv(A)
ans =

   0.147222  -0.144444   0.063889
  -0.061111   0.022222   0.105556
  -0.019444   0.188889  -0.102778

>> 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.00000   0.00000  -0.00000
  -0.00000   1.00000   0.00000
   0.00000   0.00000   1.00000

>> abs(temp * A)
ans =

   1.00000   0.00000   0.00000
   0.00000   1.00000   0.00000
   0.00000   0.00000   1.00000

数据绘制(Plotting Data)

>>  t=[0:0.01:0.98];
>>  t
t =

 Columns 1 through 11:

   0.00000   0.01000   0.02000   0.03000   0.04000   0.05000   0.06000   0.07000   0.08000   0.09000   0.10000

 Columns 12 through 22:

   0.11000   0.12000   0.13000   0.14000   0.15000   0.16000   0.17000   0.18000   0.19000   0.20000   0.21000

 Columns 23 through 33:

   0.22000   0.23000   0.24000   0.25000   0.26000   0.27000   0.28000   0.29000   0.30000   0.31000   0.32000

 Columns 34 through 44:

   0.33000   0.34000   0.35000   0.36000   0.37000   0.38000   0.39000   0.40000   0.41000   0.42000   0.43000

 Columns 45 through 55:

   0.44000   0.45000   0.46000   0.47000   0.48000   0.49000   0.50000   0.51000   0.52000   0.53000   0.54000

 Columns 56 through 66:

   0.55000   0.56000   0.57000   0.58000   0.59000   0.60000   0.61000   0.62000   0.63000   0.64000   0.65000

 Columns 67 through 77:

   0.66000   0.67000   0.68000   0.69000   0.70000   0.71000   0.72000   0.73000   0.74000   0.75000   0.76000

 Columns 78 through 88:

   0.77000   0.78000   0.79000   0.80000   0.81000   0.82000   0.83000   0.84000   0.85000   0.86000   0.87000

 Columns 89 through 99:

   0.88000   0.89000   0.90000   0.91000   0.92000   0.93000   0.94000   0.95000   0.96000   0.97000   0.98000

>>  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');
>>  legend('cos','sin');
>>  legend('sin','cos');
>>  title('my plot');

>>  print -dpng 'myPolt.png'
>>  close

>>  figure(1); plot(t,y1);
>>  figure(2); plot(t,y2,'r');

>>  subplot(1,2,1);
>>  plot(t,y1)
>>  subplot(1,2,2)
>>  plot(t,y2,'r')
>>  title myPlot
>>  axis([0.5 1 -1 1])
>>  clf;

>>  A=magic(5);
>>  imagesc(A);
>>  imagesc(A), colorbar, colormap gray;
>>  imagesc(magic(15)), colorbar, colormap gray;

控制语句:for,while,if语句(Control Statements)

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

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

      2
      4
      8
     16
     32
     64
    128
    256
    512
   1024

>>  i=1
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)
ans =  999
>>  v(1)=2;
>>  if v(1)==1,
>     disp('The value is one');
>   elseif v(1) == 2,
>     disp('The value is two');
>   else
>     disp('The value is not one or two');
>   end;
The value is two

函数的定义和使用(Defining and using functions)

>>  function y = squareThisNumber(x);
>   y = x^2;
>   end;
>> 
>>  squareThisNumber(5)
ans =  25

>>  % octave serch path (advanced/optional)
>>  addpath('C:\users\');

>>  % you can also put the function into the 'suareThisNumber.m' file under addpath
>>  % then you can invoke this function use 'squareThisNumber(5)'

>>  function [y1,y2] = squareAndCubeThisNumber(x);
>     y1 = x^2;
>     y2 = x^3;
>   end;
>>  squareAndCubeThisNumber(3)
y1 =  9
y2 =  27
ans =  9
>>  [a,b]=squareAndCubeThisNumber(3)
y1 =  9
y2 =  27
a =  9
b =  27
>>  a
a =  9
>>  b
b =  27

>>  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]
theta =

   0
   1

>>  function J = costFunctionJ(X, y, theta)
>   % X is the "design matrix" containing out training examples.
>   % y is the class labels
>
>   m = size(X, 1);                     % number of traning examples
>   predictions = X * theta;            % predictions of hypothesis on all m examples
>   sqrErrors = (predictions - y).^2;   % squared errors
>
>   J = 1/(2*m) * sum(sqrErrors);
>   end;
>>  j=costFunctionJ(X,y,theta)
j = 0

>>  size(X,1)
ans =  3
>>  X*theta
ans =

   1
   2
   3

>>  sum(X)
ans =

   3   6

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

>>  sum(A)
ans =

   369   369   369   369   369   369   369   369   369

>>  sum(sum(A))
ans =  3321

>>  theta=[0;0]
theta =

   0
   0

>>  j=costFunctionJ(X,y,theta)
j =  2.3333
>>  (1^2 + 2^2 + 3^2)/(2*3)
ans =  2.3333
posted @ 2019-04-04 12:53  KerShaw  阅读(667)  评论(0编辑  收藏  举报