Octave commands (CS229)

Documentation

Octave GNU documentation

File operation

load <fileName> will load the data file.

save <fileName> <variableName> -ASCII will save variable to the file as ASCII format.

Checking for virables

who command will list all variable names in the current scope, further more, whos command will list all variable details as a table with attribute name, variable size, bytes and data type.

clear <variableName> will release the attribute.

size(varName) shows the size of variable.

Data type

Matrix

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

means
$$A=\begin{bmatrix}
1 && 2 \\
3 && 4 \\
5 && 6
\end{bmatrix}_{3\times2}$$

  • Abstract
    1
    B = A([1 3],:);

$$B=\begin{bmatrix}
1 && 2\\
5 && 6
\end{bmatrix}$$

Get the first and the third row for all columns from A matrix as B matrix.

  • Diagonal matrix
    eye(N) is an $N\times N$ diagonal matrix.

  • Ones and Zeros
    ones(m, n) means a matrix of $m$ rows and $n$ columns with all elements equal to one, similarly, zeros(m, n) stands for a matrix of $m$ rows and $n$ columns with all elements equal to zero.

  • Transpose

A' is the transpose matrix of A matrix.

Draw

hist(var, N) will draw a histagram with variable var an divide into N pieces.

plot(x, y) will draw a figure of (x,y) function.

1
2
3
4
5
6
7
8
plot(x, y1);
hold on;
plot(x, y2, 'r');
xlabel('x-axis name')
ylabel('y-axis name')
legend('y1', 'y2')
title('figure name')
print -dpng 'myPlot.png'

The code above will draw y1 and y2(in red color) function together corresponding to x.

print will save the figure as a file.

close will close the figure.

1
2
figure(1); plot(x, y1);
figure(2); plot(x, y2);

The code above will plot the figures in two different figure with the given name.

1
2
3
4
5
subplot(1, 2, 1); % Divides plot a 1x2 grid, and access the first element. Will only draw the axises.
plot(x, y1); % Draw (x, y1) in the empty axises above
axis([xBeginVal xEndVal yBeginVal yEndVal]); % Reset the axises range of the figure above.
subplot(1, 2, 2); % Access the second element
plot(x, y2);
1
2
3
D = magic(15);
imagesc(D);
imagesc(D), colorbar, colormap gray;

Help

help <commandName> can check the command usage.