>> sz = size(A); % actually, size return a 1x2 matrix >> size(sz) ans =
1 2
>> size(A, 1) % get the first dimension of A (i.e. the number of rows) ans = 3 >> size(A, 2) % the number of columns ans = 2
length(): return the size of the longest dimension.
1 2 3 4 5
>> length(A) % get the size of the longest dimension. Confusing, not recommend ans = 3 >> v = [1, 2, 3, 4]; >> length(v) % We often length() to get the length of a vector ans = 4
Load data
We can use basic shell commands to find data that we want.
1 2 3 4 5 6 7 8 9 10 11
>> pwd ans = /Users/c >> cd MyProg/octave/ >> pwd ans = /Users/c/MyProg/octave >> ls featureX.dat featureY.dat >> ls -l total 16 -rw-r--r-- 1 c staff 188 Sep 8 10:00 featureX.dat -rw-r--r-- 1 c staff 135 Sep 8 10:00 featureY.dat
load command can load data from a file.
1 2
>> load featureX.dat >> load('featureY.dat')
The data from file is now comed into matrices after load
>> for i = 1: 10, > v(i) = 2^i; > end; >> v' ans =
2 4 8 16 32 64 128 256 512 1024
while
1 2 3 4 5 6 7 8 9 10
>> i = 1; >> while i <= 5, > v(i) = 100; > i = i + 1; > end; >> v' ans =
100 100 100 100 100 64 128 256 512 1024
if
1 2 3 4 5 6 7 8 9
>> for i = 1: 10, > if v(i) > 100, > disp(v(i)); > end; > end; 128 256 512 1024
Or, we can program like this,
1 2 3 4 5 6 7 8
x = 1; if (x == 1) disp ("one"); elseif (x == 2) disp ("two"); else disp ("not one or two"); endif
break & continue
1 2 3 4 5 6 7 8
i = 1; while true, v(i) = 999; i = i + 1; if i == 6, break; end; end;
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13
v =
999 999 999 999 999 64 128 256 512 1024
Function
Create a Function
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:
1 2 3 4
function y = squareThisNumber(x)
y = x^2;
To call this function in Octave, do either:
cd to the directory of the functionName.m file and call the function:
1 2 3 4 5
% Navigate to directory: cd /path/to/function
% Call the function: functionName(args)
Add the directory of the function file to the load path:
1 2 3 4 5
% 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
Function with multiple return values
Octave’s functions can return more than one value:
1 2 3 4 5
function [square, cube] = squareAndCubeThisNumber(x)
square = x^2; cube = x^3;
1 2 3
>> [s, c] = squareAndCubeThisNumber(5) s = 25 c = 125
Practice
Let’s say I have a data set that looks like this, with data points at (1, 1), (2, 2), (3, 3). And what I’d like to do is to define an octave function to compute the cost function J of theta for different values of theta.
First, put the data into octave:
1 2 3
X = [1, 1; 1, 2; 1, 3] % Design matrix y = [1; 2; 3] theta = [0; 1]
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
X =
1 1 1 2 1 3
y =
1 2 3
theta =
0 1
Then define the cost function:
1 2 3 4 5 6 7 8 9 10 11 12 13
% costFunctionJ.m
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 erroes
J = 1 / (2*m) * sum(sqrErrors);
Now, use the costFunctionJ:
1 2
>> j = costFunctionJ(X, y, theta) j = 0
Got j = 0 because we set theta as [0; 1] which is fitting our data set perfectly.
Vectorization
Vectorization is the process of taking code that relies on loops and converting it into matrix operations. It is more efficient, more elegant, and more concise.
As an example, let’s compute our prediction from a hypothesis. Theta is the vector of fields for the hypothesis and x is a vector of variables.
If you recall the definition multiplying vectors, you’ll see that this one operation does the element-wise multiplication and overall sum in a very concise notation.