Examples
Using Octave
First, follow the installation guide to install Octave on your system. Then, launch the interactive prompt by typing octave
in a terminal or by clicking the icon in the programs menu. For further guidance, see the manual page on Running Octave.
Language Basics
Click the headings below to see a manual page related to each topic
Variable Assignment
Assign values to variables with =
(Note: assignment is pass-by-value)
Comments
%
starts a comment block that continues to the end of the line.
Evaluation
The output of every command is printed to the console unless terminated with ;
. The disp()
command can be used to print output anywhere. Use exit
to quit the console.
Math
Many mathematical operators are available in addition to the standard arithmetic. Operations are floating-point.
Matrices
Arrays in Octave are called matrices. One-dimensional matrices are referred to as vectors. Use space
or ,
to separate elements and ;
to start a new row.
Linear Algebra
Many common linear algebra operations are simple to program using Octave’s matrix syntax.
Accessing Elements
Octave is 1-indexed. Matrix elements are accessed as matrix(rowNum, columnNum)
.
Iteration
Octave supports for
and while
loops, as well as other control flow structures.
Vectorization
For-loops can often be replaced or simplified using vector syntax. The operators *
,/
,^
,%
all support element-wise operations using .
. Many other functions operate element-wise by default (sin
,+
,-
, etc.).
Plotting
plot
can be called with vector arguments to create 2D line and scatter plots.
Strings
Strings are simply arrays of characters. Strings can be composed using printf
-style formatting with sprintf
and fprintf
.
If-else
Conditional statements can be used to create branching logic in your code.
Help
The help
and doc
commands can be invoked at the Octave prompt to print documentation for any function.
Packages
Community-developed packages can be added from the Octave Forge to extend the functionality of Octave’s core library. (Matlab users: Forge packages act similarly to Matlab’s toolboxes.) The pkg
command is used to manage these packages. For example, to use the image processing library from the Forge, use: