Course notes on Octave operations.
Operations in Octave
1 | A = [1 2; 3 4; 5 6;] |
To load data into Octave
- cd to the file that contains the data files
- load filename.dat or load(‘filename.dat’)
who
To visualize your data in Octave1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32>> t = [0:0.01:0.98];
>> y1 = sin(2*pi*4*t);
>> plot(t,y1) #t as x, y1 as y
>> y2 = cos(2*pi*4*t);
>> plot(t, y2)
>> hold on; #plot two y together
>> plot(t, y1);
>> plot(t, y2, 'r'); #color in red
>> plot(t, y1, 'b'); # color in blue
>> xlabel('time')
>> ylabel('value')
>> legend('sin', 'cos') #add legend
>> title('my plot') #add title
>> cd Desktop; print -dpng 'myPlot.png' #save the plot to designated place
>> close #close the plot window
>> figure(1); plot(t, y1) #two separate figures
>> figure(2); plot(t, y2)
>> subplot(1, 2, 1) #divides plot into a 1*2 grid, access the first element
>> plot(t, y1) #plot the figure at the first element place
>> subplot(1, 2, 2) #divides plot into a 1*2 grid, access the second element
>> plot(t, y2) #plot at the second element place
>> axis([0.5 1 -1 1]) # for the second plot, change x axis to (0.5, 1), y axis to (-1. 1)
>> clf #clears a figure to blank
>> imagesc(A), colorbar, colormap gray #convert matrix into colormap. colorbar shows the legend of colr. colormap gray defines to be black and white.
>> a=1, b=2, c=3 #comma chaining of commands
a = 1
b = 2
c = 3
>> a = 1; b = 2; c = 3; #same as above, except it does not print
Control statement: for, while, if
1 | #for loop |
If you want to exit Octave, you can just type exit
and enter, it will cause Octave to quit.
To define a function in Octave, we need to creat a file in the working directory named functioname.m
, and store the following definition in it. e.g.:1
2function y = squareThisNumber(x)
y = x^2
Then you can call directly in Octave1
2>> squareThisNumber(5)
ans = 25
We can also add a search path to Octave, so we don’t have to save the function file in the working directory1
addpath('Users/joannaouyang/Desktop')
Octave allows you to return multiple values for a function1
2
3
4function [y1, y2] = squareAndCubeThisNumber(x)
y1 = x^2;
y2 = x^3;
Back to Octave1
2
3>> [a, b] = squareAndCubeThisNumber(5)
a = 25
b = 125
pre-defined cost function j1
2
3
4
5
6
7
8function J = costFunctionJ(X, y, theta)
# X is the 'design matrix' containing our training examples
# y is the class label
m = size(X, 1); # numberof training examples
predictions = X * theta; #predictions of hyputhesis on all m examples
sqrErrors = (predictions - y).^2; #squared errors
J = 1 / (2 * m) * sum(sqrErrors);
Go back to Octave1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17>> x = [1 1; 1 2; 1 3]
x =
1 1
1 2
1 3
>> y = [1; 2; 3]
y =
1
2
3
>> theta = [0; 1];
>> j = costFunction(x, y, theta)
j = 0 #because with theta0 = 0, theta1 = 1, the line predict perfectly
Vectorization
To simplify the code and speed up running
1 | # Unvectorized implementation |
Gradient descent function vectorized implementation1
2