1. 程式人生 > 實用技巧 >[Machine Learning] Octave Computing on Data

[Machine Learning] Octave Computing on Data

Mutiplate materix:

Everytime you see '.' mean element wise operator.

>> A = [1 2; 3 4; 5 6];
>> B = [11 12; 13 14; 15 16];
>> C = [1 1; 2 2];

>> A*C
ans =

    5    5
   11   11
   17   17


>> A .* B # each element of A & B (1*11=11)
ans =

   11   24
   39   56
   75   96

>> A .^ 2
ans =

    1    4
    9   16
   25   36
>> 1 ./ A
ans =

   1.00000   0.50000
   0.33333   0.25000
   0.20000   0.16667

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

   1
   2
   3

>> log(v)
ans =

   0.00000
   0.69315
   1.09861

>> exp(v)
ans =

    2.7183
    7.3891
   20.0855
>> abs(v)
ans =

   1
   2
   3

>> -v
ans 
= -1 -2 -3

Increase v element all by 1:

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

   2
   3
   4

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

   1
   1
   1
"""

or

>> v + 1
ans =

   2
   3
   4

Transposed:

>> A
A =

   1   2
   3   4
   5   6

>> A'
ans =

   1   3   5
   2   4   6

Max for vector:

>> 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 for materix: column wise max value

>> A
A =

   1   2
   3   4
   5   6

>> max(A)
ans =

   5   6

>> A
A =

   8   1   6
   3   5   7
   4   9   2

>> max(A, [], 1) # column wise max value
ans =

   8   9   7

>> max(A, [], 2) # row wise max value
ans =

   8
   7
   9

If you want to find the max value of the whole matrix:

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

Logic:

>> a
a =

    1.00000   15.00000    2.00000    0.50000

>> a < 3
ans =

   1   0   1   1

find: find all the indexs match the conds:

>> find(a < 3)
ans =

   1   3   4

Magic materix:

>> A = magic(3)
A =

   8   1   6
   3   5   7
   4   9   2

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

   1
   3

c =

   1
   2

A(1, 1) A(3, 2) have the element which larger than 7

Sum:

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

    1   15    2    0

>> ceil(a)
ans =

    1   15    2    1

Column wise sum:

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

row wise sum:

>> sum(A, 2)
ans =

   369
   369
   369
   369
   369
   369
   369
   369
   369

Diagonal sum:

>> A .* eye(9). # only get diagonal values
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(sum(A .* eye(9)))
ans =  369

Other way around:

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

    0    0    0    0    0    0    0    0   45
    0    0    0    0    0    0    0   44    0
    0    0    0    0    0    0   43    0    0
    0    0    0    0    0   42    0    0    0
    0    0    0    0   41    0    0    0    0
    0    0    0   40    0    0    0    0    0
    0    0   39    0    0    0    0    0    0
    0   38    0    0    0    0    0    0    0
   37    0    0    0    0    0    0    0    0

Inverse:

>> A = magic(3);
>> 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

# A' * A = I