Basics


In [1]:
5+6
3-2
2^6
1 == 2  % False
1 ~= 2  % ~= not equals


ans =  11
ans =  1
ans =  64
ans = 0
ans = 1

In [2]:
1 && 0  % and
1 || 0  % or
xor(1,0)  % xor


ans = 0
ans = 1
ans = 1

In [3]:
a = 3;  % semicolon suppresses output

In [4]:
b = 'hi'
c = (3 > 1)


b = hi
c = 1

In [5]:
a = pi


a =  3.1416

In [6]:
disp(a);  % octaves version of print


 3.1416

In [7]:
disp(sprintf('2 decimals: %0.2f', a))  % c style printf


2 decimals: 3.14

In [8]:
format long  % defaults strings to long format
a


a =  3.14159265358979

In [9]:
format short  % revert
a


a =  3.1416

Matrixes


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


A =

   1   2
   3   4
   5   6


In [11]:
v = [1, 2, 3]  % 1x3  row vector


v =

   1   2   3


In [12]:
v = [1; 2; 3]  % 3x1 column vector


v =

   1
   2
   3


In [13]:
v = 1:0.1:2  % v from 1 to 2 step 0.1
% returns a row vector


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 [14]:
v = 1:6


v =

   1   2   3   4   5   6


In [15]:
ones(2,3)


ans =

   1   1   1
   1   1   1


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


C =

   2   2   2
   2   2   2


In [17]:
w = ones(1, 3)


w =

   1   1   1


In [18]:
w = zeros(1, 3)


w =

   0   0   0


In [19]:
w = rand(1, 3)  % between 0 and 1


w =

   0.95201   0.54374   0.66103


In [20]:
w = randn(1, 3)  % from gausian dist


w =

  -3.0221e-04  -8.6436e-01  -5.4188e-01


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

In [22]:
hist(w)



In [23]:
hist(w, 50)  % 50 bins



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


ans =

Diagonal Matrix

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


In [25]:
help eye


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

 -- eye (N)
 -- eye (M, N)
 -- eye ([M N])
 -- 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 [26]:
A


A =

   1   2
   3   4
   5   6


In [27]:
size(A)  % returns a 1x2 matrix


ans =

   3   2


In [28]:
size(A, 1)  % num of rows
size(A, 2)  % num of cols


ans =  3
ans =  2

In [29]:
v = [1, 2, 3, 4];
length(v)  % returns longest dimension


ans =  4

In [30]:
pwd  % current directory


ans = /home/gary/Devel/python/coursera_ml

In [31]:
cd '/home/gary/Devel/python/coursera_ml' % change directory

In [32]:
ls


01_Matrices_Review_py.ipynb   featuresX.dat  octave-workspace
01_Matrixes_Review_oct.ipynb  hello.mat      priceY.dat
02_Octave_Tutorial.ipynb      LICENSE	     README.md

In [33]:
load featuresX.dat
load priceY.dat
% load('featuresX.dat) equivalent

In [34]:
who  % variable in workspace


Variables in the current scope:

A          a          b          featuresX  v
C          ans        c          priceY     w


In [35]:
size(featuresX)


ans =

   47    2


In [36]:
whos  % detail view of who


Variables in the current scope:

   Attr Name           Size                     Bytes  Class
   ==== ====           ====                     =====  ===== 
        A              3x2                         48  double
        C              2x3                         48  double
        a              1x1                          8  double
        ans            1x2                         16  double
        b              1x2                          2  char
        c              1x1                          1  logical
        featuresX     47x2                        752  double
        priceY        47x1                        376  double
        v              1x4                         32  double
        w              1x10000                  80000  double

Total is 10163 elements using 81283 bytes


In [37]:
% clears featuresX   % remove from workspace

In [38]:
v = priceY(1:10)


v =

   399900
   329900
   369000
   232000
   539900
   299900
   314900
   198999
   212000
   242500


In [39]:
save hello.mat v;  % add --ascii for plain text

Indexing


In [40]:
A


A =

   1   2
   3   4
   5   6


In [41]:
A(3,2)


ans =  6

In [42]:
A(2,:)  % Everything in the second row


ans =

   3   4


In [43]:
A(:,2)  % Everything in the second column


ans =

   2
   4
   6


In [44]:
A([1,3],:)  % rows 1 and 3


ans =

   1   2
   5   6


In [45]:
A(:,2) = [10; 11; 12]  % Replace a column


A =

    1   10
    3   11
    5   12


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


A =

     1    10   100
     3    11   101
     5    12   102


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


ans =

     1
     3
     5
    10
    11
    12
   100
   101
   102


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


A =

   1   2
   3   4
   5   6

B =

   11   12
   13   14
   15   16


In [55]:
C = [A B]  % Concatenation  
% [A B] = [A, B]


C =

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


In [56]:
C = [A; B]  % Concat at the bottom


C =

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


In [ ]: