octave syntax

This article records my Octave learning notes, keeps developing…

Chinese Version: Octave是Matlab开源版本,这篇文章记录我对Octave学习心得和基本使用方法,以便以后手生的时候快速上手。

Basic Operations

  1. tilde equal ~= is in octave, Octave uses bang equal !=

  2. Octave uses single quote to present string assignment b = ‘hi’

  3. Like Matlab, semicolon suppressing(抑制) output.

  4. disp(str) for printing string. disp(sprintf(‘2 decimals: %0.2f’, a)) for printing format string.
  5. format long/short
  6. row vector [1 2 3]
  7. column vector [1; 2; 3]
  8. ones(2,3) => initialise matrix
  9. rand(1,3) => uniform distribution from 0 to 1: gaussian distribution with mean zero and variance/standard deviation equal to one; gaussian random number/ normal random number.
  10. I = eye(5) => Identity Matrix
  11. help eye
  12. example
    1
    w = -6 + sqrt(10)*(randn(1,10000000));
    hist(w)
    hist(w,50) => print 50bucket

Moving Data Around

  1. size, length

    1
    size(A) = rows columns
    size(A,1) = rows
    size(A,2) = columns
    length(A) = longest dimension
  2. load data, find data

  3. pwd {show current path}/ cd ‘path’ / ls
  4. difference between mat and dat
  5. load file.data = load(‘file.dat’)
  6. who/ whos{describe the details}
  7. clear / clear variable
  8. save hello.mat {dat?}
  9. v = priceY(1:10) => the first 10 elements, column order
  10. I(1:5) / I(1,3)
    octave:47> B = I(1:5)
    B =
    1 0 0 0 1
    octave:48> I
    I =
    Diagonal Matrix

    1 0 0
    0 1 0
    0 0 1

  11. save file.mat v; {binary format, compress?}

  12. save file.txt v -ascii; % save as text (ASCII)

  13. manipulation

  14. A(3,2) A(2,:) % “:” means every element along that row/column
  15. A([1 3; :]) % all first and third columns
  16. Append
    A is a 3 by 2 matrix
    A = [A, [100; 200; 300] ]
    Now, A is 3 by 3
    A = [A; [100, 200, 300]]
    Now, A is 4 by 3
  17. A(:) % put all elements of A into a single column vector
    Now, A is a 12 by 1 column vector
  18. concatenation

C = [A B] / [A, B]
C = [A; B] % ; means go to the next line

Computing on data

  1. element-wise operation
    . .^ ./
    log(v) exp(v) abs(v) -v %equals -1
    v
    v + 1 % v + ones(length(v),1)

  2. Matrix transpose, A = (A’)’

  3. Function max

    1
    a is a row vector:
    max(a)
    [value, index] = max(a)
    A is a matrix:
    max(A) % column wise max
  4. logic

    1
    a = [1 15 2 0.5]
    a < 3 = [1 0 1 1]
    find(a < 3) % list the index of element satisfy the given condition
    ans = [1 3 4]
  5. magic(n) => create an N by N magic square, magic(2) is not defined. magic square => each row, each column, and diaglog sum up the same value

    1
    octave:64> A
    A =
       8   1   6
       3   5   7
       4   9   2
    octave:65> find(A>=5) {return the index checking by column}
    ans =
       1
       5
       6
       7
       8
    octave:66> [r c]=find(A>=5)
    r =
       1
       2
       3
       1
       2
    c =
       1
       2
       2
       3
       3
  6. column-wise & element-wise

    1
    2
    sum(A) prod(A)   {A is matrix, column wise}
    floor(A)% round down ceil(A)%round up {element-wise}
  7. rand(1,3)% create 1 by 3; rand(3)% create 3 by 3

  8. max(A,B) % A and B has same size, maximum on element wise

    1
    octave:84> A = magic(3)
    A =
       8   1   6
       3   5   7
       4   9   2
    octave:85> max(A)
    ans =
       8   9   7
    octave:86> max(A,[],1) % = max(A), 1 means the first dimension, column
    ans =
       8   9   7
    octave:87> max(A,[],2) % 2 means the second dimension, row
    ans =
       8
       7
       9
    octave:89> max(A,5)
    ans =
       8   5   6
       5   5   7
       5   9   5
    octave:90> min(A,5)
    ans =
       5   1   5
       3   5   5
       4   5   2
    find the entire maximum is A: max(max(A))     or     max(A(:))
  9. Function Sum

    1
    A = magic(9)
    sum(A,1) % sum column
    sum(A,2) % sum row
    sum the main diagonal
         sum(sum(A .* eye(9)))
    sum the other diagonal: using function flipud, flip up an down
         sum(sum(A .* flipud(eye(9))))
  10. sudep inverse
    pinv(A)

    1
    octave:93> A
    A =
       8   1   6
       3   5   7
       4   9   2
    octave:94> inv(A)
    ans =
       0.147222  -0.144444   0.063889
      -0.061111   0.022222   0.105556
      -0.019444   0.188889  -0.102778
    octave:95> pinv(A)
    ans =
       0.147222  -0.144444   0.063889
      -0.061111   0.022222   0.105556
      -0.019444   0.188889  -0.102778
    octave:96> A*ans
    ans =
       1.0000e+00  -1.2212e-14   6.5503e-15
       5.5511e-17   1.0000e+00  -1.1102e-16
      -5.8842e-15   1.2434e-14   1.0000e+00
    octave:97> inv(A)
    ans =
       0.147222  -0.144444   0.063889
      -0.061111   0.022222   0.105556
      -0.019444   0.188889  -0.102778
    octave:98> A*ans
    ans =
       1.00000   0.00000  -0.00000
      -0.00000   1.00000   0.00000
       0.00000   0.00000   1.00000

Plotting Data

  1. Plot 1
    1
    octave:131> x = [0:0.01:1];
    octave:132> plot(x)
    octave:133> xlabel('x number times')
    octave:134> ylabel('value of x')
    octave:135> legend('x')
    octave:136> title('plot1')
    octave:137> print -dpng 'plot1.png’

  1. Plot 2
    1
    close
    figure(1); plot…
    figure(2); plot….
    subplot(1,2,1) %Divides plot a 1x2 grid, access the first element
    axis([ 0.5 1 - 1 1]);
    clf; % clear figure

  1. Draw Matrix
    1
    2
    3
    4
    5
    6
    octave:173> magic(3)
    ans =
    8 1 6
    3 5 7
    4 9 2
    octave:174> plot(magic(3))

1
imagesc(magic(5)), colorer, colormap gray;     {commands/comma chaining of function }

Control Statement, Functions

  1. while true,
    break;
  2. Octave search path
    addpath(path);
  3. function [y1,y2] = …. % return multiple values

Vectorization

numerical and linear algebra library in octave, C++, java

Cost Function Example

Vectorisation Octave Code

Vectorisation C++ Code

Linear Regression Example