Octave is a high-level language suitable for prototyping learning algorithms.
Octave is primarily intended for numerical computations and provides extensive graphics capabilities for data visualization and manipulation. Octave is normally used through its interactive command line interface, but it can also be used to write non-interactive programs. The syntax is matrix-based and provides various functions for matrix operations. This tool has been in active development for over 20 years.
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Octave - Prototyping Machine Learning Algorithms
1. No Rights Reserved.
... a simple toolkit for
prototyping ML applications
craigtrim@gmail.com
June, 2014
2. No Rights Reseved.2
Installation
Octave is a high-level language suitable for
prototyping learning algorithms.
Octave is primarily intended for numerical
computations and provides extensive graphics
capabilities for data visualization and
manipulation. Octave is normally used through its
interactive command line interface, but it can also
be used to write non-interactive programs.
The syntax is matrix-based and provides various
functions for matrix operations.
Octave has been in development for over 20
years
For instructions on setting up the latest version on
Cygwin
– http://www.gnu.org/software/octave/
For the latest executable binary (3.6.4, used in
this tutorial)
• http://preview.tinyurl.com/k3n6s4u
3. No Rights Reseved.3
Variable Assignments
>> a = 3;
The semicolon suppresses output at the prompt.
Supressing output is useful when working with
very large matrices.
>> c = (3 >= 1);
>> c
1
If the output of a variable is supressed, it can be
examined at any time by typing that variable's
name on the command prompt.
References:
GNU Tutorial:
– http://www.gnu.org/software/octave/doc/inter
preter/Variables.html#Variables
– The name of a variable must be a sequence
of letters, digits and underscores, but it may
not begin with a digit.
– Octave does not enforce a limit on the length
of variable names
– The following are all valid variable names
• x
• x15
• __foo_bar_baz__
• fucnrdthsucngtagdjb
– Case is significant in variable names. The
symbols a and A are distinct variables.
– There is one built-in variable with a special
meaning. The ans variable always contains
the result of the last computation.
4. No Rights Reseved.4
Examining Variables in Memory
who
whos
Both of these commands will show the in-memory
variables.
After running the last operation, we have
>> whos
Variables in the current scope:
Attr Name Size Bytes Class
==== ==== ==== ===== =====
c 1x1 1 logical
Total is 1 element using 1 byte
Variables can be cleared from memory using the clear
command:
>> clear c
>> whos
After running this command, I type in whos again, and
nothing happens.
References:
GNU Tutorial:
– http://www.gnu.org/software/octave/doc/interprete
– When creating simple one-shot programs it
can be very convenient to see which
variables are available at the prompt. The
functions
• who
• whos
• whos_line_format
will show different information about what is
in memory.
5. No Rights Reseved.5
Displaying Variables
>> a = pi;
>> a
3.1416
>> disp(sprintf('%0.2f', a))
3.14
>> format long
>> a
3.14159265358979
>> format short
>> a
3.1416
The short datatype is default.
References:
GNU Tutorial:
6. No Rights Reseved.6
Elementary Math Operations
>> 21+21
42
>> 6*7
42
>> 84/2
42
>> 2^5+2^4-2^3+2
42
References:
GNU Tutorial:
– http://www.gnu.org/software/octave/doc/interprete
– Unless otherwise noted, all of the functions
described in this chapter will work for real
and complex scalar, vector, or matrix
arguments. Functions described as mapping
functions apply the given operation
individually to each element when given a
matrix argument. For example:
7. No Rights Reseved.7
Logical Operations
>> 2 == 2
1
>> 1 == 2
0
>> 1 ~= 2
1
>> 1 && 0
0
>> 1 || 0
1
>> xor(1, 0)
1
References:
GNU Tutorial:
– http://www.gnu.org/software/octave/doc/interpreter/Logica
• Octave has built-in support for logical values, i.e.,
variables that are either true or false.
– http://www.gnu.org/software/octave/doc/interpreter/Boolea
• An element-by-element boolean expression is a
combination of comparison expressions using the
boolean operators “or” (‘|’), “and” (‘&’), and “not” (‘!’),
along with parentheses to control nesting
8. No Rights Reseved.8
Matrix Generation:
Manual Assignment of a Matrix
Assigning Matrices:
>> A = [ 1 2 3 ; 4 5 6 ; 7 8 9 ]
1 2 3
4 5 6
7 8 9
The semicolon tells Octave to go to the next row of the
matrix.
References:
GNU Tutorial:
9. No Rights Reseved.9
Matrix Generation:
Manual Assignment of a Vector
>> V = [ 1 2 3 4 5 6 7 8 9 0 ]
1 2 3 4 5 6 7 8 9 0
This is a 1x10 matrix, also known as a row vector.
If I wanted to create a 10x1 matrix, I would issue this command
in Octave:
>> V = [1 ; 2 ; 3 ; 4 ; 5 ; 6 ; 7 ; 8 ; 9 ; 0 ]
1
2
3
4
5
6
7
8
9
0
Conceptual Review:
A vector is:
– An ordered collection of numbers.
– A special type of matrix (rectangular array of
numbers).
– A column matrix, meaning that you have just one
column and a certain number of rows.
When you create a vector:
– you write the numbers in a column surrounded by
brackets.
The names of vectors:
– are usually written as single, boldfaced, lowercase
letters.
The size of a vector:
– is determined by its rows or how many numbers it
has.
References:
GNU Tutorial:
10. No Rights Reseved.10
Matrix Generation:
Assigning Ranges
>> v = [0:50]
0 1 2 3 4 5 6 7 8 … 50
This creates a vector that starts at element 0, ends at
element 50, and increments by 1 (the default value). It is
possible to use ranges and specify an increment.
Note that Elements 12 – 49 are not shown to save space.
Commands:
v = [ x : y ]
x = start
y = end
References:
GNU Tutorial:
11. No Rights Reseved.11
Matrix Generation:
Assigning Ranges using Increments
>> v = [0:5:50]
0 5 10 15 20 25 30 35 40 45 50
This creates a vector that starts at element 0, ends at
element 50, and increments by 5.
The increment can be any value we want:
>> v = [0:pi:50]
0.00000 3.14159 6.28319 9.42478
12.56637 15.70796 18.84956 21.99115
25.13274 28.27433 31.41593 34.55752
37.69911 40.84070 43.98230 47.12389
>> v = [0:e:50]
0.00000 2.71828 5.43656 8.15485
10.87313 13.59141 16.30969 19.02797
21.74625 24.46454 27.18282 29.90110
32.61938 35.33766 38.05595 40.77423
43.49251 46.21079 48.92907
Commands:
v = [ x : n : y ]
x = start
y = end
n = increment
References:
GNU Tutorial:
12. No Rights Reseved.12
Matrix Generation:
Generating Matrices of 0’s and 1’s
>> zeros(4,4)
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
>> ones(4,4)
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
Matrix generations can be used in variable assignments:
>> A = ones(2,5)
1 1 1 1 1
1 1 1 1 1
The use of the ones or zeros keyword is not limited to matrices. I can
use the same for vectors:
>> v = zeros(1,9)
0 0 0 0 0 0 0 0 0
>> v = ones(1,9)
1 1 1 1 1 1 1 1 1
Commands:
A = zeros(x, y)
A = ones(x, y)
References:
GNU Tutorial:
13. No Rights Reseved.13
Matrix Generation:
Generating n-Value Matrices (1-2)
Simply create a ones matrix, and multiply by n, where n is
any number:
>> 9 * ones(9, 9)
9 9 9 9 9 9 9 9 9
9 9 9 9 9 9 9 9 9
9 9 9 9 9 9 9 9 9
9 9 9 9 9 9 9 9 9
9 9 9 9 9 9 9 9 9
9 9 9 9 9 9 9 9 9
9 9 9 9 9 9 9 9 9
9 9 9 9 9 9 9 9 9
9 9 9 9 9 9 9 9 9
Or using decimals
>> 0.25 * ones(2,5)
0.25000 0.25000 0.25000 0.25000 0.25000
0.25000 0.25000 0.25000 0.25000 0.25000
Commands:
A = n * ones(x, y);
References:
GNU Tutorial:
14. No Rights Reseved.14
Matrix Generation:
Generating n-Value Matrices (2-2)
Generating a matrix with irrational numbers:
>> C = pi * ones(3,3)
3.1416 3.1416 3.1416
3.1416 3.1416 3.1416
3.1416 3.1416 3.1416
>> format long
>> C
3.14159265358979 3.14159265358979 3.14159265358979
3.14159265358979 3.14159265358979 3.14159265358979
3.14159265358979 3.14159265358979 3.14159265358979
Note the use of format long to display the elements with
more precision.
Commands:
A = n * ones(x, y);
References:
GNU Tutorial:
15. No Rights Reseved.15
Matrix Generation:
The Identity Matrix
This will create a multiplicative identity matrix – defined as
a square matrix with a diagonal of ones running from top-
left to lower-right.
>> eye(4)
Diagonal Matrix
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
Commands:
A = eye(x, y)
References:
GNU Tutorial:
Conceptual Review:
What is an identity matrix?
– Matrices can be square, identity, triangular, singular - or not.
– The two different types of identity matrices are somewhat
related to the two identity numbers in arithmetic.
• The additive identity in arithmetic is 0. If you add 0 to
a number, you don’t change the number - the number
keeps its identity.
– The same idea works for the multiplicative identity: The
multiplicative identity in arithmetic is 1.
• You multiply any number by 1, and the number keeps
its original identity.
– The multiplicative identity for matrices has one thing in
common with theadditive identity and, then, one big difference.
• The common trait of the multiplicative identity is that
the multiplicative identity also comes in many sizes;
• The difference is that the multiplicative identity comes
in only one shape: a square.
• A square matrix is n × n; the number of rows and
number of columns is the same.
Octave will let you create a non-square identity matrix
– but in this case, it's not a proper identity matrix if it's not square.
16. No Rights Reseved.16
Matrix Generation:
The Magic Matrix
I can use this command for MxN matrices:
A = fix(rand(3, 5) * 10)
2 0 9 8 6
3 0 2 7 9
1 5 6 1 1
but this is easier for square (NxN) matrices:
A = magic(5)
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
Note that all magic matrices have to be square, so I still
need a fix(rand(x,y)*10) command if I want a non-square
matrix.
Commands:
w = magic(x, y);
Conceptual Overview:
Magic matrices have a mathematical property that all their
rows and columns and diagonals sum up to the same
thing.
While the use of a magic matrix may not have any
immediate application in machine learning, it's a helpful
way of generating a matrix of whole numbers.
References:
GNU Tutorial:
17. No Rights Reseved.17
Matrix Generation:
Random Values
>> rand(4,4)
0.729803 0.236942 0.161466 0.023865
0.639122 0.146757 0.689484 0.255327
0.864042 0.412367 0.541050 0.556997
0.560884 0.254363 0.260782 0.734587
This generates random values into either a matrix
(above), or a vector (below.
>> v = rand(1,5)
0.94561 0.25730 0.77948 0.58087 0.69059
If I want to generate a square matrix of random elements,
I can omit the y value, and simply type:
>> rand(4)
0.57236 0.50242 0.32187 0.63812
0.32134 0.85426 0.26143 0.61013
0.35396 0.92741 0.66957 0.32677
0.29163 0.66729 0.84041 0.82068
to generate a 4x4 matrix.
Commands:
w = rand(x, y);
References:
GNU Tutorial:
18. No Rights Reseved.18
Matrix Generation:
Negative Values (1-2)
All the examples from the previous slide apply, but the use
of randn will be permitted to generate negative values.
Not all values will be negative.
For example:
>> randn(4)
0.204915 -2.161812 1.225799 0.927005
-0.416605 1.092304 0.998591 -0.221070
0.314358 0.874479 0.153042 -0.065082
-0.754553 -1.529642 0.316105 -0.547542
Or as this sequence demonstrates:
>> w = randn
0.31763
>> w = randn
0.14623
>> w = randn
-0.56862
Commands:
w = randn(x, y);
References:
GNU Tutorial:
19. No Rights Reseved.19
Matrix Generation:
Negative Values (2-2)
If I want to make a scalar, vector or matrix negative, it's
simply a matter of prefixing this value with a negative sign.
>> v = fix(rand(1,5)*10)
7 7 5 5 7
>> -v
-7 -7 -5 -5 -7
and I can make it positive by using the abs command.
>> abs(-v)
7 7 5 5 7
Commands:
w = randn(x, y);
References:
GNU Tutorial:
20. No Rights Reseved.20
Mathematic Operations:
Matrix Multiplication
Given two matrices:
>> A = [ randperm(3);
randperm(3) ]
2 1 3
1 3 2
>> B = [ randperm(2);
randperm(2);
randperm(2)]
2 1
1 2
2 1
I can multiply them to get C:
>> C = A * B
11 7
9 9
Multiplying a 2x3 matrix with a 3x2 matrix creates a 2x2
square matrix.
Commands:
References:
GNU Tutorial:
21. No Rights Reseved.21
Mathematic Operations:
Element-Wise Operations (1-3)
Element-wise means acting upon each element in a
matrix individually.
>> A = fix(rand(3,5) * 10)
8 2 6 1 4
3 2 5 3 8
9 0 7 0 2
>> B = fix(rand(3,5) * 10)
3 9 3 5 8
2 7 7 3 3
4 3 9 6 7
>> C = A .* B
24 18 18 5 32
6 14 35 9 24
36 0 63 0 14
Commands:
References:
GNU Tutorial:
22. No Rights Reseved.22
Mathematic Operations:
Element-Wise Operations (2-3)
>> B = fix(rand(3, 5) * 10) + 1
9 1 5 9 4
8 2 8 7 5
9 10 5 5 5
>> 1 ./ B .* B
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
multiplying the reciprocal by itself gives a product matrix of
ones.
Commands:
A .^ B
1 ./ v
Conceptual Review:
A mathematical expression or function so related to
another that their product is one; the quantity obtained by
dividing the number one by a given quantity.
References:
GNU Tutorial:
23. No Rights Reseved.23
Mathematic Operations:
Element-Wise Operations (3-3)
>> a = fix(rand(1,5) * 10)
1 7 9 4 1
>> a > 3
0 1 1 1 0
A vector is returned with truth values for this comparison.
The first and last elements of the original vector are less
than three.
Commands:
Conceptual Review:
References:
GNU Tutorial:
24. No Rights Reseved.24
Mathematic Operations:
Absolute Values
To retrieve the absolute value for any single element, vector of
matrix, enclose the value in abs(...).
>> abs(randn(3))
0.53333 0.66756 1.22765
0.68927 1.07300 0.30291
0.75810 2.71484 0.95232
Or, for single values:
>> w = randn();
>> c = abs(w);
>> w
-0.96150
>> c
0.96150
Note the use of
w = randn();
or
w = rand;
Both expressions accomplish the same purpose.
Commands:
A = abs(x);
References:
GNU Tutorial:
25. No Rights Reseved.25
Mathematic Operations:
Computing Logarithms
the parameter to this command can be a scalar, vector or
matrix.
>> A = fix(rand(2,4) * 10)+1
3 2 8 8
7 7 4 6
>> log(A)
1.09861 0.69315 2.07944 2.07944
1.94591 1.94591 1.38629 1.79176
Or use log2
>> w = 2^10
w = 1024
>> log2(w)
ans = 10
Commands:
log(v)
References:
GNU Tutorial:
26. No Rights Reseved.26
Matrix Operations:
The Size Command (1-2)
>> eye(5)
Diagonal Matrix
1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1
>> A = eye(5);
>> size(A)
5 5
In this example, we create a 5x5 identity matrix, and then
get the size from Octave. It returns 1x2 column matrix
(vector) containing the values [5 5]. Since the answer is
returned as a matrix, we can get the size of this too:
>> size(size(A))
1 2
The size of the vector containing two elements is 1x2.
That is, there is one row and two columns. The answer to
this initial size command is returned as a 1x2 matrix.
Commands:
A = size(A, DIM)
References:
GNU Tutorial:
27. No Rights Reseved.27
Matrix Operations:
The Size Command (2-2)
As a Java developer, I find the following feature of Octave
unique, and very useful. A function in Octave can return
multiple variables from a function.
Let's test this on size:
>> [x,y] = size(rand(2,6));
x = 2
y = 6
We create a random 2x6 matrix, and immediately take the
size; assigning this to a vector of x and y. X is the number
of rows (2) and y is the number of columns (6).
We can access these two variables as needed from
memory. Rather than creating an eplicit structure to hold
multiple values, the language is fully vectorized.
I can also use the size command to find the number of
rows or columns in a matrix. Given this randomly
generated 2x3 matrix:
>> M = [randperm(3,3); randperm(3,3);]
3 2 1
1 2 3
Commands:
A = size(A, DIM)
References:
GNU Tutorial:
28. No Rights Reseved.28
Matrix Operations:
Finding the number of Rows and Columns
Assuming we start with this 2x3 matrix:
>> A = eye(2,3)
1 0 0
0 1 0
Finding the number of Rows in a Matrix:
>> size(M, 1)
2
Finding the number of Columns in a Matrix:
>> size(M, 2)
3
Commands:
size(A, 1) use this for rows
size(A, 2) use this for columns
References:
GNU Tutorial:
29. No Rights Reseved.29
Matrix Operations:
Finding the Maximum Value (1-2)
The max command returns the maximum value from a
vector or matrix
Given this vector
>> a = [ 50, 29, 283, 12, -94, 8, 0, 24 ];
>> max(a)
283
This is also a helpful command
>> [val, ind] = max(a)
val = 283
ind = 3
val holds the value of the maximum element and ind is the
index into its position within the vector.
Commands:
References:
GNU Tutorial:
30. No Rights Reseved.30
Matrix Operations:
Finding the Maximum Value (2-2)
This can also be applied to a matrix:
>> A = fix(rand(3,5)*10)+1
4 2 8 4 9
6 7 7 7 10
2 9 4 8 7
>> [val,ind] = max(A)
val = 6 9 8 8 10
ind = 2 3 1 3 2
In this case, a vector is created that holds the maximum
values in the matrix. The index may be a little hard to read
at first, but it's a vector. Each element position in the
vector is the column, the value of the element at that
position is the row. So the first element of "2" is column 1,
row 2 and the maximum value here is 6.
If I wanted to return a single maximum value for a matrix,
just use max twice:
>> [val,ind] = max(max(A))
val = 10
ind = 5
Commands:
References:
GNU Tutorial:
31. No Rights Reseved.31
Matrix Operations:
Length of a Matrix or Vector (1-2)
Assuming we start with this 2x3 matrix:
>> A = eye(2,3)
1 0 0
0 1 0
For matrix objects, the length is the number of rows or
columns, whichever is greater.
For a vector, which is always a 1xN matrix, this definition
makes sense. For any non 1xN matrix, not so much.
Using the 2x3 matrix M, defined above, length returns the
longest dimension
>> length(M)
3
Assuming we start with this vector:
>> v = (1:5)
1 2 3 4 5
>> length(v)
5
Commands:
length(A)
References:
GNU Tutorial:
32. No Rights Reseved.32
Matrix Operations:
Length of a Matrix or Vector (2-2)
Assume this matrix
>> M
3 1 6 7 5 4 9 2 10 8
5 2 7 8 3 9 1 6 10 4
7 4 10 5 3 8 2 1 6 9
This command
length(M(3,:)
gives me the length of the third row.
This command
6:length(M(3,:)
gives me the elements 6 – 10 (where 10 is the length of the
row)
So the entire command
M(3, 4:length(M(3,:)))
gives me the elements 4-10 of the third row of matrix M.
Commands:
length(A)
References:
GNU Tutorial:
33. No Rights Reseved.33
Matrix Operations:
Indexing a Matrix Range (1-3)
Let's say I create a really large vector of data:
>> v = [1:500];
I can slice off any of the elements I want.
>> s = v(1:10)
1 2 3 4 5 6 7 8 9 10
Of course, I don't have to assign this output to a variable.
I can use this command to examine the contents on-the-fly:
>> v(25:30)
25 26 27 28 29 30
And this works for matrices too:
>> M = [ randperm(10,10);
randperm(10,10);
randperm(10,10)]
3 1 6 7 5 4 9 2 10 8
5 2 7 8 3 9 1 6 10 4
7 4 10 5 3 8 2 1 6 9
This command creates three 1x10 vectors with a random permutation
of 1:10, where each value is unique (view help randperm for more
information). These three vectors are appended together into a single
3x10 matrix.
References:
GNU Tutorial:
34. No Rights Reseved.34
Matrix Operations:
Indexing a Matrix Range (2-3)
Given matrix M I can “slice” out any column:
>> M
3 1 6 7 5 4 9 2 10 8
5 2 7 8 3 9 1 6 10 4
7 4 10 5 3 8 2 1 6 9
and reutrn the first column:
M(:,1)
3 1 6 7 5 4 9 2 10 8
5 2 7 8 3 9 1 6 10 4
7 4 10 5 3 8 2 1 6 9
or return the second column:
M(:,2)
3 1 6 7 5 4 9 2 10 8
5 2 7 8 3 9 1 6 10 4
7 4 10 5 3 8 2 1 6 9
or return the third column:
M(:,3)
3 1 6 7 5 4 9 2 10 8
5 2 7 8 3 9 1 6 10 4
7 4 10 5 3 8 2 1 6 9
etc. Note that the returned “slice” is a 3x1 matrix (column vector) in
this case, not a 1x3 row vector. This makes sense, as we're taking a
slice from the column dimension of the matrix.
Commands:
M(:,n)
References:
GNU Tutorial:
35. No Rights Reseved.35
Matrix Operations:
Indexing a Matrix Range (3-3)
I can slice out any row dimension using equivalent syntax:
Given the same 3x10 matrix M, let's return the third row:
>> M(3,:)
3 1 6 7 5 4 9 2 10 8
5 2 7 8 3 9 1 6 10 4
7 4 10 5 3 8 2 1 6 9
If you only want the first n elements of a given row, use this syntax:
>> M(3,1:5)
3 1 6 7 5 4 9 2 10 8
5 2 7 8 3 9 1 6 10 4
7 4 10 5 3 8 2 1 6 9
or
>> M(3, 6:length(M(3,:)))
3 1 6 7 5 4 9 2 10 8
5 2 7 8 3 9 1 6 10 4
7 4 10 5 3 8 2 1 6 9
Commands:
M(n, :)
References:
GNU Tutorial:
36. No Rights Reseved.36
Matrix Operations:
Indexing a Specific Element
f I want to index into a specific element (or cell) of a
Matrix, just use x and y
>> M(3,5)
3 1 6 7 5 4 9 2 10 8
5 2 7 8 3 9 1 6 10 4
7 4 10 5 3 8 2 1 6 9
In handwritten notation, this is denoted as
Commands:
M(x, y)
References:
GNU Tutorial:
37. No Rights Reseved.37
Matrix Operations:
Flattening a Matrix
Given a 5x2 Matrix denoted Z:
>> Z = [ 1 2 ; 3 4 ; 5 6 ; 7 8 ; 9 0 ]
1 2
3 4
5 6
7 8
9 0
I can flatten this into a single 10x1 column vector:
>> v = Z(:)
1
3
5
7
9
2
4
6
8
0
Commands:
M(:)
References:
GNU Tutorial:
38. No Rights Reseved.38
Matrix Operations:
Adding Rows and Columns to a Matrix
Given:
>> M
3 1 6 7 5 4 9 2 10 8
5 2 7 8 3 9 1 6 10 4
7 4 10 5 3 8 2 1 6 9
I can add a column to this matrix:
>> [x,y] = size(M);
>> M(:,y+1) = [0, 0, 0]
3 1 6 7 5 4 9 2 10 8 0
5 2 7 8 3 9 1 6 10 4 0
7 4 10 5 3 8 2 1 6 9 0
I can add a row to this matrix:
>> M(x+1,:) = [zeros(11,1)]
3 1 6 7 5 4 9 2 10 8 0
5 2 7 8 3 9 1 6 10 4 0
7 4 10 5 3 8 2 1 6 9 0
0 0 0 0 0 0 0 0 0 0 0
I could also have written that command as:
M(x+1,:) = [zeros(length(M),1)]
Commands:
References:
GNU Tutorial:
39. No Rights Reseved.39
Matrix Operations:
Adding Values to Rows and Columns
Given this Vector
>> v = fix(rand(1,5) * 10)
2 0 4 6 6
how do I increment each value by 1?
>> v + ones(size(v))
3 1 5 7 7
Create a ones vector and add it to the existing vector. I
can increment the original vector by any value n, simply
by multiplying the ones vector by n before adding it.
>> v + ones(size(v)) * 5
7 5 9 11 11
In this case, I have added 5 to each element in vector v.
Commands:
References:
GNU Tutorial:
40. No Rights Reseved.40
Matrix Operations:
Rotating a Matrix
Assuming we start with this matrix:
>> A = eye(4,4)
Diagonal Matrix
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
We can rotate this matrix 90° to the right:
>> flipud(A)
Permutation Matrix
0 0 0 1
0 0 1 0
0 1 0 0
1 0 0 0
Commands:
A = flipud(x)
flipud means “flip up/down”
References:
GNU Tutorial:
41. No Rights Reseved.41
Matrix Operations:
Matrix Transpose (1-3)
>> A = fix(rand(2,4)*10)
6 6 4 3
3 3 0 3
>> A'
6 3
6 3
4 0
3 3
The transpose of the sum of two matrices is equal to the
sum of the two transposes:
>> (A + A)' == A' + A'
1 1
1 1
1 1
1 1
Remember that in order for two matrices to be added, the
dimensions must be equal
Commands:
References:
GNU Tutorial:
42. No Rights Reseved.42
Matrix Operations:
Matrix Transpose (2-3)
The transpose of the product of two matrices is equal to the product of
the two transposes in the opposite order:
>> A = fix(rand(3,3) * 10)
4 4 0
4 3 8
8 7 2
>> B = fix(rand(3,3) * 10)
5 6 8
8 3 2
1 4 6
>> (A*B)'==B' * A'
1 1 1
1 1 1
1 1 1
Because matrix dimensions must be equal in multplication, and we are
multplying both the original form and the transposed form, I can't see
any way for this to work other than the use of square matrices.
Implied in this rule is that multiplication is possible (the dimensions fit).
Here are two matrices, A and B, their product, the transpose of their
product, and the product of their transposes (in reverse order).
Commands:
References:
GNU Tutorial:
43. No Rights Reseved.43
Matrix Operations:
Matrix Transpose (3-3)
Matrix multiplication is not generally commutative, so
multiplying AT * BT does not give you the same result as
multiplying in the reverse order.
>> A
4 5 9
3 7 4
0 6 1
>> B
5 6 8
8 3 2
1 4 6
>> A' * B'
38 41 16
115 73 69
77 86 31
>> B' * A'
69 75 49
75 55 22
96 62 18
Commands:
References:
GNU Tutorial:
44. No Rights Reseved.44
Matrix Operations:
Creating Matrix Subsets
>> a = [1 7 9 4 1];
>> find(a > 3)
2 3 4
>> A = fix(rand(5,3) * 10)
3 2 2
5 0 5
9 7 0
4 4 7
0 6 1
>> find(A > 3)
2
3
4
8
9
10
12
14
Using find on a matrix will return a column vector of
elements that meet the criteria.
Commands:
References:
GNU Tutorial:
45. No Rights Reseved.45
Control Operations:
For Loop
Let's create a for loop to demonstrate powers of 2. We'll put powers of
two up to n into a vector.
>> n = 30;
>> v = (zeros(n, 1));
>> for i = 1:n,
v(i) = 2^i;
end;
2
4
8
16
32
64
128
256
...
268435456
536870912
1073741824
Note that I could leave this out – where I initialize the vector to zeros.
Without this line, the vector would default to a row vector (1xN matrix).
Because it's easier to display a long list of numbers as a column vector, I
prefer to initialize the vector first to a Nx1 matrix (column vector).
Commands:
References:
GNU Tutorial:
46. No Rights Reseved.46
Control Operations:
While Loop
>> x = 1; y = 10;
>> v = zeros(y, x);
>> while x <= y, v(x) = 42;
x = x + 1;
end;
42
42
42
42
42
42
42
42
42
42
Again, a preference for creating a column
vector (YxX Matrix)
Commands:
References:
GNU Tutorial:
47. No Rights Reseved.47
Control Operations:
Break Statements
>> i = 1;
>> while true,
v(i) = 42;
i++,
if i==5,
break;
end;
end;
42 42 42 42
Any control statement can be formatted onto a single line:
>> while true, v(i) = 42; i++, if i==5,
break; end; end;
42 42 42 42
Commands:
References:
GNU Tutorial:
48. No Rights Reseved.48
Functions:
Creating a Function (1-3)
Create this function as a file called “sq.m”:
function y = sq(x)
y = x^2;
Save this to a path on your computer. I prefer to use my
base installation path for Octave, and a sub-directory called
“functions”:
C:SoftwareOctave-3.6.4functions
If I navigate to this directory, I can call this function from
Octave like this:
>> x = sq(42)
1764
Commands:
References:
GNU Tutorial:
49. No Rights Reseved.49
Functions:
Creating a Function (2-3)
A preferred approach is to add this directory to the search
path for Octave. Once I do this, I can navigate any where on
the file system within Octave, and the intepreter will still be
able to find the functions that I have defined in saved in the
above path.
addpath(
"c:/Software/Octave-3.6.4/functions")
I prefer to add this to the octaverc file. This is located at
C:SoftwareOctave3.6.4shareoctavesitem
startupoctaverc
on my installation.
Each time Octave starts up, this search path will be loaded.
I'm going to quit Octave, restart, and try this.
And now it works.
Commands:
References:
GNU Tutorial:
50. No Rights Reseved.50
Functions:
Creating a Function (3-3)
Let's create another useful function. Earlier in this tutorial, I
was generating matrices of uneven dimensions, where each
element was a whole number greater than 0.
Let's take that code, and define a function:
function y = gen(x,y)
y = fix(rand(x,y) * 10) + 1;
and save this to a file called “gen.m”.
I can use this right away within Octave:
>> gen(5,2)
2 2
3 2
6 5
3 7
6 5
Commands:
References:
GNU Tutorial:
51. No Rights Reseved.51
Functions:
Returning Multiple Values
Octave will allow us return multiple values from a function
without the need to define an explict structure to contain
them.
I'm going to create a file called bam.m:
function [a, b, c, d] = bam(x)
a = x^2;
b = x^3;
c = x^4;
d = x^5;
When I call this from the Octave intepreter, I assign the
results into a vector:
>> [v1, v2, v3, v4] = bam(10)
v1 = 100
v2 = 1000
v3 = 10000
v4 = 100000
Commands:
Notes
Although I've defined the function as assigning values to 4
variables, I don't need to use these same variable names
when calling the function. I can if I want, but it's not a
requirement.
Likewise, although the function is assigning values to four
variables, I don't need to use all of these when calling the
function.
References:
GNU Tutorial:
52. No Rights Reseved.52
Functions:
Returning Multiple Values
I can invoke
>> [x,y] = bam(42)
x = 1764
y = 74088
or
>> [x, y, z] = bam(42)
x = 1764
y = 74088
z = 3111696
or
>> [x, y, z, BLAH] = bam(42)
x = 1764
y = 74088
z = 3111696
BLAH = 130691232
This syntax is rather convenient when you need to return
multiple values.
Commands:
References:
GNU Tutorial:
53. No Rights Reseved.53
File I/O:
Basic Commands
Note that on the Windows O/S, if you want to use the
change
directory command, it's been to use this format:
cd “C:/Users/SSD256/Desktop/”
capture the folder in double quotes, and use forward slashes
(rather than the more awkward double back-slashes).
The Octave interpreter does not appear to be able to handle
enviornment variables, eg
cd %var%
Commands:
pwd print working directory
ls list directory
cd change directory
References:
GNU Tutorial:
54. No Rights Reseved.54
File I/O:
Loading Data
I have a file on my root directory with training data. The file is named
featureX.dat
>> load featuresX.dat
This file has been loaded into a variable named “featuresX” (the name of
the file). I can interact with this variable:
>> whos featuresX
Variables in the current scope:
Attr Name Size Bytes Class
==== ==== ==== ===== =====
featuresX 43180x2 690880 double
Total is 86360 elements using 690880 bytes
>> size(featuresX)
43180 2
>> [x,y] = size(featuresX)
x = 43180
y = 2
Note that whos gives all the information that size does.
However, if all you're after are the column dimensions, then using the
[x,y] assignment shown in the last assessment is certainly easier than
trying to parse data from whos.
Commands:
load <filename>
References:
GNU Tutorial:
55. No Rights Reseved.55
File I/O:
Saving Data
Assuming I've loaded “featuresX.dat”, I can save a subset to
disk using this syntax:
>> v = featuresX(2:10);
>> save temp.dat v
It appears that Octave requires data be assigned to a
variable prior to being written to file.
This will not work:
>> save temp.dat featuresX(2:10)
warning: save: no such variable
'featuresX(2:10)‘
This will work
>> v = featuresX(2:10);
>> save temp.dat v
Commands:
save <filename> <variable>
References:
GNU Tutorial:
56. No Rights Reseved.56
File I/O:
Clearing Data
I can clear all memory by simply typing
clear
I can clear a specific variable, say the file I just loaded, by
typing
clear(featuresX)
and then I can run whos to verify, and no output will result.
I can also use regular expressions to identify variables to
clear.
Commands:
clear(x)
References:
GNU Tutorial:
57. No Rights Reseved.57
Data Visualization:
Histograms (1-2)
>> w = -6 + sqrt(42) * (randn(1,10000));
>> hist(w)
Commands:
hist(w)
References:
GNU Tutorial:
58. No Rights Reseved.58
Data Visualization:
Histograms (2-2)
Adding labels, and making the graph pretty:
>> hold on
>> title "My First Histogram"
>> xlabel "time"
>> ylabel "space"
>> legend ('amount')
Note the use of the “hold on” command. This basically tells
the compiler: hold on there pal, I got more to add …
You will see later that these commands apply to all
visualizations we create in Octave.
Commands:
hist(w)
References:
GNU Tutorial:
59. No Rights Reseved.59
Data Visualization:
Color Mapping (1-4)
>> A = magic(5);
>> imagesc(A);
Commands:
imagesc(A);
References:
GNU Tutorial:
60. No Rights Reseved.60
Data Visualization:
Color Mapping (2-4)
>> imagesc(magic(50)), colorbar; Commands:
imagesc(A), colorbar;
References:
GNU Tutorial:
61. No Rights Reseved.61
Data Visualization:
Color Mapping (3-4)
>> imagesc(magic(50)),
colorbar,
colormap gray;
Commands:
imagesc(A), colorbar, colormap gray;
References:
GNU Tutorial:
62. No Rights Reseved.62
Data Visualization:
Color Mapping (4-4)
Let's take another look at this:
>> A = magic(5);
>> imagesc(A), colorbar, colormap gray;
and then if we slice out a cell from this visualized matrix:
>> A(3,3)
ans = 13
We can see that cell 3,3 on the visualization has a middle
shade of gray that corresponds with values in the colorbar
Commands:
imagesc(A), colorbar, colormap gray;
References:
GNU Tutorial:
63. No Rights Reseved.63
Appendix
Matrixes vs Matrices?
– The tendency is to prefer the foreign plural if a word is used in a technical sense, and to
prefer the english plural in a non-technical sense.
– Therefore:
• mathematical formulae v. formulas for success
• mathematical indices v. consumer price indexes
• mathematical appendices v. appendixes
• and hence … matrices v. matrixes
– prov:wasQuotedFrom
• http://www.oxforddictionaries.com/us/definition/american_english/matrix