05: Octave and MATLAB

[ 05: Octave and MATLAB ]

Previous Next Index

Video Lectures

 

Installation

 

Basic Operation

Changing Octave Prompt

%% Changing Octave Prompt
PS1('>>');
%% Change working directory in windows example:
cd 'c:/path/to/desired/directory name'
%% Note that it uses normal slashes and does not uses escape characters for the empty spaces.

Elementary Operations

%% elementary operations
5+6

ans =  11

3-2

ans =  1

5*8

ans =  40

1/2

ans =  0.50000

2^6

ans =  64

1 == 2  % false

ans = 0

1 ~= 2  % true. note, not "!="

ans =  1

1 && 0

ans = 0

1 || 0

ans =  1

xor(1,0)

ans =  1

Variable Assignment

%% variable assignment
a = 3;

b = 'hi';

c = 3>=1;

% Displaying them:

a = pi;

disp(a)

3.1416

disp(sprintf('2 decimals: %0.2f', a))

2 decimals: 3.14

disp(sprintf('6 decimals: %0.6f', a))

6 decimals: 3.141593

format long

a

a =  3.14159265358979

format short

a

a =  3.1416

Vectors and Matrices

%% vectors and matrices

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

A =

   1   2

   3   4

   5   6

v = [1 2 3]

v =

   1   2   3

v = [1; 2; 3]

v =

   1

   2

   3

%% from 1 to 2, with stepsize of 0.1. Useful for plot axes

v = [1:0.1:2]

v =

   1.0000   1.1000   1.2000   1.3000   1.4000   1.5000   1.6000   1.7000   1.8000   1.9000   2.0000

%% from 1 to 6, assumes stepsize of 1 (row vector)

v = 1:6

v =

   1   2   3   4   5   6

% same as C = [2 2 2; 2 2 2]

C = 2*ones(2,3)

C =

   2   2   2

   2   2   2

% 1x3 vector of ones

w = ones(1,3)

w =

   1   1   1

w = zeros(1,3)

w =

   0   0   0

% drawn from a uniform distribution

w = rand(1,3)

w =

   0.558722   0.596400   0.056719

w = randn(1,3)

w =

  -0.23185   2.32378   0.52952

% (mean = -6, var = 10) - note: add the semicolon

w = -6 + sqrt(10)*(randn(1,10000));

% plot histogram using 10 bins (default)
hist(w)

Plot histogram using 50 bins

hist(w,50)  % plot histogram using 50 bins
% note: if hist() crashes, try “graphics_toolkit(‘gnu_plot’)”

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

Moving Data Around

Data files are used in this sectionfeaturesX.datpriceY.dat

Matrix and Dimensions

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

A =

   1   2

   3   4

   5   6

%% dimensions

% 1×2 matrix: [(number of rows) (number of columns)]

sz = size(A)

sz =

   3   2

%% dimensions

% 1×2 matrix: [(number of rows) (number of columns)]

sz = size(A)

sz =

   3   2

% number of rows

size(A,1)

ans =  3

% number of cols

size(A,2)

ans =  2

% size of longest dimension

v = [1 2 3]

v =

   1   2   3

v = [1; 2; 3]

v =

   1

   2

   3

length(v)

ans =  3

Loading Data

%% Loading Data
% show current directory (current path)

pwd
ans = /home/pi

pwd
ans = /home/pi

ls
bcm2835-1.16	     Desktop	gather_gps.py  opencv-3.0.0		  python_games			sw_linux
bcm2835-1.16.tar.gz  Documents	indiecity      opencv-3.1.0		  raspberry-pi-starter-project	tf
book		     Downloads	octave-core    parse-embedded-sdks-1.0.0  sketchbook

% change directory
cd Downloads
ls
featuresX.dat  opencv-3.1.0.zip  priceY.dat

load priceY.dat % alternatively, load('priceY.dat')
load featuresX.dat

% list variables in workspace
who
Variables in the current scope:

A          C          ans        featuresX  priceY     v
B          a          c          ind        r          val

whos
Variables in the current scope:

   Attr Name           Size                     Bytes  Class
   ==== ====           ====                     =====  =====
        A              9x9                        648  double
        B              3x2                         48  double
        C              2x2                         32  double
        a              1x4                         32  double
        ans            1x8                          8  char
        c              3x1                         12  double
        featuresX     27x2                        432  double
        ind            1x1                          8  double
        priceY        27x1                        216  double
        r              3x1                         12  double
        v              3x1                         24  double
        val            1x2                         16  double

Total is 196 elements using 1488 bytes

v = featuresX(1:10)
v =

   2104   1600   2400   1416   3000   1985   1534   1427   1380   1494

v = priceY(1:10)
v =

   3999
   3299
   3690
   2320
   5399
   2999
   3149
   1989
   2120
   2425

% save variable v into file hello.mat
save hello.mat v;

ls
featuresX.dat  hello.mat  opencv-3.1.0.zip  priceY.dat
% save as ascii
save hello.txt v -ascii;
ls
featuresX.dat  hello.mat  hello.txt  opencv-3.1.0.zip  priceY.dat

% fopen, fread, fprintf, fscanf also work  [[not needed in class]]

Indexing Matrix

%% indexing
% indexing is (row,col)
A(3,2)
ans =  78

% get the 2nd row.
A(2,:)
ans =

   57   68   79    9   11   22   33   44   46

% ":" means every element along that dimension

% print all  the elements of rows 1 and 3
A([1 3],:)
ans =

   47   58   69   80    1   12   23   34   45
   67   78    8   10   21   32   43   54   56

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

   1   2
   3   4
   5   6

% change second column
A(:,2) = [10; 11; 12]
A =

    1   10
    3   11
    5   12

% print all  the elements of rows 1 and 3
A([1 3],:)
ans =

    1   10
    5   12

A(2,:)
ans =

    3   11

% ":" means every element along that dimension

% append column vec
A = [A, [100; 101; 102]];
A = [A, [100; 101; 102]]
A =

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

% Select all elements as a column vector.
A(:)
ans =

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

% Putting data together
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

% same dims as A
C = [A B]
C =

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

% concatenating A and B matrices side by side

C = [A; B]
C =

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

% Concatenating A and B top and bottom

  

Computing On Data

Initialize Variables

%% initialize variables
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

v = [1;2;3]
v =

   1
   2
   3

%% matrix operations
% matrix multiplication
A * C
ans =

    5    5
   11   11
   17   17

% element-wise multiplication
A .* B
ans =

   11   24
   39   56
   75   96

% element-wise square of each element in A
A .^ 2
ans =

    1    4
    9   16
   25   36

% element-wise reciprocal
1./v
ans =

   1.00000
   0.50000
   0.33333

% functions like this operate element-wise on vecs or matrices
log(v)
ans =

   0.00000
   0.69315
   1.09861

exp(v)
ans =

    2.7183
    7.3891
   20.0855

abs(v)
ans =

   1
   2
   3

% -1*v
-v
ans =

  -1
  -2
  -3

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

   2
   3
   4

% v + 1 % same

% matrix transpose
A'
ans =

   1   3   5
   2   4   6

%% misc useful functions
% max  (or min)
a = [1 15 2 0.5]
a =

    1.00000   15.00000    2.00000    0.50000

val = max(a)
val =  15

% val -  maximum element of the vector a and index - index value where maximum occur
[val,ind] = max(a)
val =  15
ind =  2

% if A is matrix, returns max from each column
val = max(A)
val =

   5   6

% find
a < 3
ans =

   1   0   1   1

find(a < 3)
ans =

   1   3   4

A = magic(3)
A =

   8   1   6
   3   5   7
   4   9   2

% row, column indices for values matching comparison
[r,c] = find(A>=7)
r =

   1
   3
   2

c =

   1
   2
   3

% sum, prod
sum(a)
ans =  18.500

prod(a)
ans =  15

% or ceil(a)
floor(a)
ans =

    1   15    2    0

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

   0.27772   0.74698   0.39357
   0.82662   0.76164   0.45336
   0.67367   0.92308   0.81734

% maximum along columns(defaults to columns - max(A,[]))
max(A,[],1)
ans =

   8   9   7

% maximum along rows
max(A,[],2)
ans =

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

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

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

% Matrix inverse (pseudo-inverse)
% inv(A'*A)*A'
pinv(A)
ans =

 Columns 1 through 8:

   4.5353e-04  -1.2230e-03   1.6729e-03   1.2647e-02  -1.2062e-02   3.1805e-04   3.0300e-04   2.9902e-04
   3.0111e-04   3.0111e-04   1.2801e-02  -1.2199e-02   3.0111e-04  -1.0878e-03   1.6900e-03   3.0111e-04
  -1.0706e-03   1.4019e-02  -1.2045e-02   3.0091e-04   4.5374e-04   1.4870e-04   2.8418e-04   3.1993e-04
   1.2647e-02  -1.2045e-02   3.0132e-04   3.0300e-04  -1.0725e-03   1.6729e-03   4.5353e-04   1.3176e-04
  -1.0810e-02   3.0111e-04   3.0111e-04   3.0111e-04   3.0111e-04   3.0111e-04   3.0111e-04   3.0111e-04
   2.8418e-04   4.7047e-04   1.4870e-04  -1.0706e-03   1.6747e-03   2.9923e-04   3.0091e-04   1.2647e-02
   3.0300e-04   2.8230e-04   3.1805e-04   4.5353e-04   1.4849e-04   3.0132e-04   1.2647e-02  -1.3416e-02
   3.0111e-04   3.0111e-04  -1.0878e-03   1.6900e-03   3.0111e-04   1.2801e-02  -1.2199e-02   3.0111e-04
   3.0091e-04   3.0320e-04   2.9923e-04   2.8418e-04   1.2664e-02  -1.2045e-02  -1.0706e-03   1.8253e-03

 Column 9:

   3.0132e-04
   3.0111e-04
   2.9923e-04
   3.1805e-04
   1.1412e-02
  -1.2045e-02
   1.6729e-03
-- less -- (f)orward, (b)ack, (q)uit
ans =

 Columns 1 through 8:

   4.5353e-04  -1.2230e-03   1.6729e-03   1.2647e-02  -1.2062e-02   3.1805e-04   3.0300e-04   2.9902e-04
   3.0111e-04   3.0111e-04   1.2801e-02  -1.2199e-02   3.0111e-04  -1.0878e-03   1.6900e-03   3.0111e-04
  -1.0706e-03   1.4019e-02  -1.2045e-02   3.0091e-04   4.5374e-04   1.4870e-04   2.8418e-04   3.1993e-04
   1.2647e-02  -1.2045e-02   3.0132e-04   3.0300e-04  -1.0725e-03   1.6729e-03   4.5353e-04   1.3176e-04
  -1.0810e-02   3.0111e-04   3.0111e-04   3.0111e-04   3.0111e-04   3.0111e-04   3.0111e-04   3.0111e-04
   2.8418e-04   4.7047e-04   1.4870e-04  -1.0706e-03   1.6747e-03   2.9923e-04   3.0091e-04   1.2647e-02
   3.0300e-04   2.8230e-04   3.1805e-04   4.5353e-04   1.4849e-04   3.0132e-04   1.2647e-02  -1.3416e-02
   3.0111e-04   3.0111e-04  -1.0878e-03   1.6900e-03   3.0111e-04   1.2801e-02  -1.2199e-02   3.0111e-04
   3.0091e-04   3.0320e-04   2.9923e-04   2.8418e-04   1.2664e-02  -1.2045e-02  -1.0706e-03   1.8253e-03

 Column 9:

   3.0132e-04
   3.0111e-04
   2.9923e-04
   3.1805e-04
   1.1412e-02
  -1.2045e-02
   1.6729e-03
   3.0111e-04
-- less -- (f)orward, (b)ack, (q)uit

ans =

 Columns 1 through 8:

   4.5353e-04  -1.2230e-03   1.6729e-03   1.2647e-02  -1.2062e-02   3.1805e-04   3.0300e-04   2.9902e-04
   3.0111e-04   3.0111e-04   1.2801e-02  -1.2199e-02   3.0111e-04  -1.0878e-03   1.6900e-03   3.0111e-04
  -1.0706e-03   1.4019e-02  -1.2045e-02   3.0091e-04   4.5374e-04   1.4870e-04   2.8418e-04   3.1993e-04
   1.2647e-02  -1.2045e-02   3.0132e-04   3.0300e-04  -1.0725e-03   1.6729e-03   4.5353e-04   1.3176e-04
  -1.0810e-02   3.0111e-04   3.0111e-04   3.0111e-04   3.0111e-04   3.0111e-04   3.0111e-04   3.0111e-04
   2.8418e-04   4.7047e-04   1.4870e-04  -1.0706e-03   1.6747e-03   2.9923e-04   3.0091e-04   1.2647e-02
   3.0300e-04   2.8230e-04   3.1805e-04   4.5353e-04   1.4849e-04   3.0132e-04   1.2647e-02  -1.3416e-02
   3.0111e-04   3.0111e-04  -1.0878e-03   1.6900e-03   3.0111e-04   1.2801e-02  -1.2199e-02   3.0111e-04
   3.0091e-04   3.0320e-04   2.9923e-04   2.8418e-04   1.2664e-02  -1.2045e-02  -1.0706e-03   1.8253e-03

 Column 9:

   3.0132e-04
   3.0111e-04
   2.9923e-04
   3.1805e-04
   1.1412e-02
  -1.2045e-02
   1.6729e-03
   3.0111e-04
   1.4870e-04

~
~
~
~
~
~
~
~
~
~
-- less (100%) (f)orward, (b)ack, (q)uit

 

Plotting Data

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

 Screenshot 2016-06-11 15.18.35

y2 = cos(2*pi*4*t);
hold on;  % "hold off" to turn off
plot(t,y2,'r');
xlabel('time');
ylabel('value');
legend('sin','cos');
title('my plot');
print -dpng 'myPlot.png'

Screenshot 2016-06-11 15.31.12

close;  % or, "close all" to close all figures
figure(1); plot(t, y1);
figure(2); plot(t, y2);
figure(2), clf;  % can specify the figure number
subplot(1,2,1);  % Divide plot into 1x2 grid, access 1st element
plot(t,y1);
subplot(1,2,2);  % Divide plot into 1x2 grid, access 2nd element
plot(t,y2);
axis([0.5 1 -1 1]);  % change axis scale

Screenshot 2016-06-11 15.59.01


%% display a matrix (or image)
figure;
imagesc(magic(15)), colorbar, colormap gray;

Screenshot 2016-06-11 16.13.05

% comma-chaining function calls.
a=1,b=2,c=3
a = 1
b = 2
c = 3
a=1;b=2;c=3;

Control Statements : for, while, if 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
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;
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 ro two');
end;
The value is two

Functions

To create a function, type the function code in a text editor (e.g. gedit or notepad), and save the file as “functionName.m”

Example function:

% In a text ("functionName.m")

function y = squareThisNumber(x)
y = x^2;

To call the function in Octave, do either:

1) Navigate to the directory of the functionName.m file and call the function:

% Navigate to directory:
cd /path/to/function
% Call the function:
functionName(5)
ans = 25  % ( y=5^2 = 25 )

2) Add the directory of the function to the load path and save it:

% To add the path for the current session of Octave:
addpath('/path/to/function/')
% To remember the path for future sessions of Octave, after executing addpath above, also do:
savepath

Octave’s functions can return more than one value unlike most other programming languages . Example:

% In a editor

function [y1, y2] = squareandCubeThisNo(x)
y1 = x^2
y2 = x^3

Call the above function this way :

% [a,b] = squareandCubeThisNo(x)

[a,b] = squareandCubeThisNo(5)
a
 a = 25   % (y1=5^2 = 25)
b
 b = 125  % (y2=5^3 = 125)

 

 

Cost Function Algorithm:

 CostFunction

Cost Function J(θ) – is a way to, using your training data, determine values for your θ values which make the hypothesis as accurate as possible

There is a m-file called “costFunctionJ.m”.

% File name: costFunctionJ.m
% Date: 16th June 2016

function J = costFunctionJ(x, y, theta)

% x is the "design matrix" containing our training examples.
% y is the class labels

m = size(x,1);          % number of training examples.
predictions = x*theta;  % predictions of hypothesis on all m examples
sqrErrors = (predictions-y).^2;  %squared errors

J = 1/(2*m) * sum(sqrErrors);

Call the above function this way :

x = [1 1; 1 2; 1 3]
x =

   1   1
   1   2
   1   3

y = [1; 2; 3]
y =

   1
   2
   3

theta = [0; 0]
theta =

   0
   0

j = costFunctionJ(x,y,theta)
j = 2.3333

Manual method:

(1^2 + 2^2 + 3^2)/ (2*3)
ans = 2.3333

Vectorization

Vectorization example

vectorization

Unvectorized implementation

 Lets look on the example in Octave(Matlab).

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

Lets look on the example in C++.

double prediction = 0.0;
for (int j = 0; j < n; j++)
  prediction += theta[j] * x[y];

Vectorized implementation

Lets look on the example in Octave(Matlab).

prediction = theta’ * x;

Lets look on the example in C++.

double prediction
  = theta.transpose() * x;

 

 

Gradient Descent

GradientDescent

GradientDescent02

 

 

Octave Resources

At the Octave command line, typing help followed by a function name displays documentation for a built-in function. For example, help plot will bring up help information for plotting. Further documentation can be found at the Octave documentation pages.

MATLAB Resources

At the MATLAB command line, typing help followed by a function name displays documentation for a built-in function. For example, help plot will bring up help information for plotting. Further documentation can be found at the MATLAB documentation pages.

MathWorks also has a series of videos about various MATLAB features:

Introduction to MATLAB

Learning Module Learning Goals
What is MATLAB? Introduce MATLAB
The MATLAB Environment Navigate the command line, workspace, directory, and editor
MATLAB Variables Use the assignment operator to define scalar variables
MATLAB as a Calculator Perform arithmetic calculations with scalars and functions using MATLAB syntax and order of operations.
Mathematical Functions Use MATLAB variables for input and output to functions. Examples include: COS, SIN, EXP, and NTHROOT.

Vectors

Learning Module Learning Goals
Creating Vectors via Concatenation Create vectors by entering individual elements
Accessing Elements of a Vector Access specific elements of a vector
Vector Arithmetic Perform arithmetic calculations with vectors including element-wise operations
Vector Transpose Use the transpose operator to convert between row and column vectors
Creating Uniformly Spaced Vectors (The Colon Operator) Use the colon operator syntax to create vectors given the starting and ending values and the size of the interval
Creating Uniformly Spaced Vectors (The LINSPACE Function) Use the LINSPACE function to create a vector.

Visualization

Learning Module Learning Goals
Line Plots Create a line plot of a vector and customize plot markers and colors
Annotating Graphs Label axes, add a title, and add a legend to a plot

Matrices and Arrays

Learning Module Learning Goals
Creating Matrices Create matrices by directly entering scalars
Array Creation Functions Create larger matrices and vectors with built in MATLAB functions such as ZEROS and EYE
Accessing Elements of and Array Access elements of an array including entire columns or rows using row-column indexing.
Array Size and Length Use built-in functions to determine array dimensions
Concatenating Arrays Build larger arrays from smaller ones
Matrix Multiplication Perform matrix multiplication and interpret error messages related to incompatible dimensions.

Programming

Learning Module Learning Goals
Using the MATLAB Editor Write a script in the MATLAB Editor, break code into sections to execute, and find help on functions
Logical Operators Use relational and logical operators to create logical variables for program control
Conditional Data Selection Access and change elements for a vector the meet a specified criteria
If-Else Statements Use if-else statements to control which lines of code are evaluated
For Loops Repeat a sequence of commands a specified number of times
While Loops Repeat a sequence of commands while a specified condition is true

 

Advertisements

Author: iotmaker

I am interested in IoT, robot, figures & leadership. Also, I have spent almost every day of the past 15 years making robots or electronic inventions or computer programs.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s