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 Octave
1 | >> t = [0:0.01:0.98]; |
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 | function y = squareThisNumber(x) |
Then you can call directly in Octave
1 | >> squareThisNumber(5) |
We can also add a search path to Octave, so we don’t have to save the function file in the working directory
1 | addpath('Users/joannaouyang/Desktop') |
Octave allows you to return multiple values for a function
1 | function [y1, y2] = squareAndCubeThisNumber(x) |
Back to Octave
1 | >> [a, b] = squareAndCubeThisNumber(5) |
pre-defined cost function j
1 | function J = costFunctionJ(X, y, theta) |
Go back to Octave
1 | >> x = [1 1; 1 2; 1 3] |
Vectorization
To simplify the code and speed up running
1 | # Unvectorized implementation |
Gradient descent function vectorized implementation
1 |