Octave Tutorial

In this octave tutorial I am following the Andrew Ng's Machine Learning Course. This tutorial will give me basics of the mathematical programming language that I can use it through the Machine Learning course:

Basic Operations


In [1]:
5+6


ans =  11

In [2]:
5*8


ans =  40

In [3]:
2^6


ans =  64

In [5]:
1 == 2 % False


ans = 0

In [6]:
1 ~= 2 % True


ans =  1

In [7]:
1 && 0 % AND Operator


ans = 0

In [8]:
1 || 0 % OR Operator


ans =  1

In [9]:
xor(1,0)


ans =  1

In [10]:
a = 3


a =  3

In [11]:
a = 3; % semicolong supressing output

In [12]:
b = 'hi';

In [14]:
b


b = hi

In [15]:
a= pi;

In [16]:
a


a =  3.1416

In [17]:
disp(a) % Display


 3.1416

In [18]:
disp(sprintf('2 decimals: %0.2f', a))


2 decimals: 3.14

In [19]:
disp(sprintf('6 decimals: %0.6f', a))


6 decimals: 3.141593

In [20]:
a = 3.1416


a =  3.1416

In [21]:
format long

In [22]:
a


a =  3.14160000000000

In [23]:
format short

In [24]:
a


a =  3.1416

In [25]:
A = [1 2; 3 4; 5 6]


A =

   1   2
   3   4
   5   6


In [26]:
A = [1 2;
3 4;
5 6]


A =

   1   2
   3   4
   5   6


In [27]:
v = [1 2 3]


v =

   1   2   3


In [28]:
v = [1;2;3]


v =

   1
   2
   3


In [30]:
v = 1:0.1:2


v =

 Columns 1 through 8:

    1.0000    1.1000    1.2000    1.3000    1.4000    1.5000    1.6000    1.7000

 Columns 9 through 11:

    1.8000    1.9000    2.0000


In [31]:
v = 1:6


v =

   1   2   3   4   5   6


In [33]:
# One matrix
ones(2,3)


ans =

   1   1   1
   1   1   1


In [34]:
C = 2*ones(2,3)


C =

   2   2   2
   2   2   2


In [37]:
W = zeros(1,3)


W =

   0   0   0


In [35]:
w = rand(1,3)


w =

   0.89693   0.27256   0.28233


In [36]:
rand(3,3)


ans =

   0.488288   0.734175   0.502900
   0.444771   0.093458   0.211700
   0.617218   0.394657   0.027343


In [44]:
w = -6 + sqrt(10)* (randn(1,10000));
hist(w, 100)



In [46]:
eye(4) % identity matrix 4x4


ans =

Diagonal Matrix

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


In [48]:
help eye % get help


'eye' is a built-in function from the file libinterp/corefcn/data.cc

 -- Built-in Function: eye (N)
 -- Built-in Function: eye (M, N)
 -- Built-in Function: eye ([M N])
 -- Built-in Function: eye (..., CLASS)
     Return an identity matrix.

     If invoked with a single scalar argument N, return a square NxN
     identity matrix.

     If supplied two scalar arguments (M, N), 'eye' takes them to be the
     number of rows and columns.  If given a vector with two elements,
     'eye' uses the values of the elements as the number of rows and
     columns, respectively.  For example:

          eye (3)
           =>  1  0  0
               0  1  0
               0  0  1

     The following expressions all produce the same result:

          eye (2)
          ==
          eye (2, 2)
          ==
          eye (size ([1, 2; 3, 4]))

     The optional argument CLASS, allows 'eye' to return an array of the
     specified type, like

          val = zeros (n,m, "uint8")

     Calling 'eye' with no arguments is equivalent to calling it with an
     argument of 1.  Any negative dimensions are treated as zero.  These
     odd definitions are for compatibility with MATLAB.

     See also: speye, ones, zeros.

Additional help for built-in functions and operators is
available in the online version of the manual.  Use the command
'doc <topic>' to search the manual index.

Help and information about Octave is also available on the WWW
at http://www.octave.org and via the help@octave.org
mailing list.

Moving Data Around


In [50]:
A = [1 2;
3 4;
5 6]


A =

   1   2
   3   4
   5   6


In [51]:
# Returns the Size of the matrix 3x2
size(A)


ans =

   3   2


In [52]:
size(A,2)


ans =  2

In [6]:
v = [1 2 3 4]


v =

   1   2   3   4


In [7]:
length(v)


ans =  4

In [8]:
# Who command shows the variables inside the octave workspace
who


Variables in the current scope:

A    ans  v


In [9]:
# load('data.dat')

In [10]:
# Gives more detailed information about the variables
whos


Variables in the current scope:

   Attr Name        Size                     Bytes  Class
   ==== ====        ====                     =====  ===== 
        A           3x2                         48  double
        ans         1x1                          8  double
        v           1x4                         32  double

Total is 11 elements using 88 bytes


In [11]:
# Clears the variable 
clear A

In [12]:
whos


Variables in the current scope:

   Attr Name        Size                     Bytes  Class
   ==== ====        ====                     =====  ===== 
        ans         1x1                          8  double
        v           1x4                         32  double

Total is 5 elements using 40 bytes


In [13]:
# without specifiying any name it clears all
clear

In [14]:
whos

In [15]:
A = [1 2; 3 4; 5 6]


A =

   1   2
   3   4
   5   6


In [16]:
# Indexing
A(3,2)


ans =  6

In [21]:
A(2,:) % ':' means every element along that row/column


ans =

   3   4


In [20]:
A(:,2) % Returns ever


ans =

   2
   4
   6


In [23]:
A(:,2) = [10 ; 11 ; 12] % Overwrite


A =

    1   10
    3   11
    5   12


In [25]:
A = [A, [100; 101; 102]]; % Append another column vector to right
A


A =

     1    10   100   100
     3    11   101   101
     5    12   102   102


In [26]:
size(A)


ans =

   3   4


In [27]:
A(:) % put all elements of A into a single vector


ans =

     1
     3
     5
    10
    11
    12
   100
   101
   102
   100
   101
   102


In [30]:
A = [1 2; 3 4; 5 6];
B = [11 12; 13 14; 15 16];

In [33]:
C = [A B] % Concatenate A and B to make C


C =

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


In [34]:
C = [A; B] % Adds second matrix add to bottom


C =

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


In [35]:
size(C)


ans =

   6   2


In [37]:
[A B] == [A, B]


ans =

  1  1  1  1
  1  1  1  1
  1  1  1  1

Computing on Data


In [3]:
% Defining our variables
A = [1 2; 3 4; 5 6];
B = [11 12; 13 14; 15 16];
C = [1 1; 2 2];

In [4]:
% Matrix Multiplication
A*C


ans =

    5    5
   11   11
   17   17


In [5]:
% Element wise multiplication
A.*B


ans =

   11   24
   39   56
   75   96


In [7]:
A.^2


ans =

    1    4
    9   16
   25   36


In [8]:
v = [1;2;3];
1 ./ v


ans =

   1.00000
   0.50000
   0.33333


In [9]:
log(v)


ans =

   0.00000
   0.69315
   1.09861


In [10]:
exp(v)


ans =

    2.7183
    7.3891
   20.0855


In [11]:
% Absolute value, element wise
abs(v)


ans =

   1
   2
   3


In [12]:
% Same as -1 * V
-v


ans =

  -1
  -2
  -3


In [13]:
v + ones(length(v), 1)


ans =

   2
   3
   4


In [15]:
v
v + 1


v =

   1
   2
   3

ans =

   2
   3
   4


In [16]:
% Taking the transpose
A'


ans =

   1   3   5
   2   4   6


In [17]:
a = [1 15 2 0.5];
val = max(a)


val =  15

In [19]:
% Returning the index of the max value
[val, ind] = max(a)


val =  15
ind =  2

In [20]:
% Element wise comparison
a < 3


ans =

   1   0   1   1


In [22]:
% Returns the indexes where the statement true
find(a < 3)


ans =

   1   3   4


In [23]:
A = magic(3)


A =

   8   1   6
   3   5   7
   4   9   2


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


r =

   1
   3
   2

c =

   1
   2
   3


In [25]:
A(2, 3)


ans =  7

In [26]:
a 
sum(a)


a =

    1.00000   15.00000    2.00000    0.50000

ans =  18.500

In [27]:
prod(a)


ans =  15

In [28]:
floor(a)


ans =

    1   15    2    0


In [29]:
ceil(a)


ans =

    1   15    2    1


In [30]:
max(rand(3), rand(3))


ans =

   0.59695   0.13677   0.91260
   0.55173   0.84356   0.29834
   0.69395   0.60820   0.78332


In [32]:
max(A, [], 1) % Take the first dimension of A


ans =

   8   9   7


In [33]:
max(A, [], 2)


ans =

   8
   7
   9


In [35]:
max(A) % Default


ans =

   8   9   7


In [36]:
max(max(A))


ans =  9

In [37]:
max(A(:))


ans =  9

In [38]:
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


In [40]:
sum(A,1) % Sums the columns


ans =

   369   369   369   369   369   369   369   369   369


In [41]:
sum(A,2) % Sums the rows


ans =

   369
   369
   369
   369
   369
   369
   369
   369
   369


In [43]:
% Summing up the diagonal
sum(sum(A.* eye(9) ))


ans =  369

In [48]:
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


In [45]:
% Summing up the other diagonal
sum(sum(A.*flipud(eye(9))))


ans =  369

In [47]:
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


In [49]:
A = magic(3);
% Pseudo inverse
pinv(A)


ans =

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


In [50]:
temp = pinv(A);
temp*A


ans =

   1.0000e+00   2.0817e-16  -3.1641e-15
  -6.1062e-15   1.0000e+00   6.2450e-15
   3.0531e-15   4.1633e-17   1.0000e+00

Plotting Data

Simple plots can give us a way to see if we are going well


In [51]:
t = [0:0.01:0.98];
y1 = sin(2*pi*4*t);
plot(t, y1)



In [54]:
y2 = cos(2*pi*4*t);
plot(t, y2)



In [59]:
plot(t, y1);
hold on;
plot(t, y2, 'r'); % 'r' color indicator
xlabel('time');
ylabel('value');
legend('sin', 'cos');
title('my plot');
print -dpng 'myplot.png' % Saving the plot



In [60]:
close % Dissapear a figure

In [61]:
% Opening more than one figure by definin figure
figure(1); plot(t, y1);
figure(2); plot(t, y2);



In [66]:
subplot(1,2,1); % Divides plot a 1x2 grid, access first element
plot(t,y1);
subplot(1,2,2);
plot(t,y2);
axis([.5 1 -1 1]);



In [67]:
A = magic(5)


A =

   17   24    1    8   15
   23    5    7   14   16
    4    6   13   20   22
   10   12   19   21    3
   11   18   25    2    9


In [70]:
imagesc(magic(15)), colorbar, colormap gray;


Control Statements: For, While, If Statements


In [71]:
v = zeros(10, 1)


v =

   0
   0
   0
   0
   0
   0
   0
   0
   0
   0


In [73]:
% For Syntax: 
for i=1:10,
    v(i) = 2 ^ i;
end;
v


v =

      2
      4
      8
     16
     32
     64
    128
    256
    512
   1024


In [74]:
indices = 1:10; % Explicitly defining indices 
for i=indices,
    disp(i);
end;


 1
 2
 3
 4
 5
 6
 7
 8
 9
 10

In [75]:
i = 1;
while i <= 5,
    v(i) = 100;
    i += 1;
end;
v


v =

    100
    100
    100
    100
    100
     64
    128
    256
    512
   1024


In [77]:
i = 1;
while true,
    v(i) = 999;
    i += 1;
    if i == 6,
        break; % Usage of break
    end;
end;
v


v =

    999
    999
    999
    999
    999
     64
    128
    256
    512
   1024


In [79]:
% General syntax for if
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 not one or two.

Creating functions is basically creating a file with function name.m


In [85]:
% The function should be in the same directory or we can direct it with cd
squareThisNumber(64)


ans =  4096

In [86]:
64^2


ans =  4096

In [89]:
% We can define a function returns more than one value
[a,b] = squareAndCubeThisNumber(5);
a
b


a =  25
b =  125

In [90]:
% Design matrix
X = [1 1; 1 2; 1 3];
y = [1;2;3];
theta = [0;1];

In [91]:
costFunctionJ(X, y, theta)


ans = 0

In [92]:
theta = [0;0];
costFunctionJ(X,y,theta)


ans =  2.3333

Vectorization

Unvectorized: $$ h_{\theta}(x) = \sum_{j=0}^{n} {\theta_{j}x_{j}} $$

or vectorized:

$$ h_{\theta}(x) = \theta^Tx $$

These two will give different implementations

Indexing starts from 1 in matlab/octave

Unvectorized implementation:

prediction = 0.0;
for j = 1:n+1,
    prediction += theta(j) * x(j);
end;

Vectorized implementation:

prediction = theta' * x;

In [93]:
A = [1 2; 3 4; 5 6];
B = [1 2 3; 4 5 6];

In [94]:
C = A*B


C =

    9   12   15
   19   26   33
   29   40   51