| GNU Octave Manual by John W. Eaton Paperback (6"x9"), 324 pages, 4 figures ISBN 0954161726 RRP £19.99 ($29.99) |
4.1 Matrices
It is easy to define a matrix of values in Octave. The size of the matrix is determined automatically, so it is not necessary to explicitly state the dimensions. The expression
a = [1, 2; 3, 4]
results in the matrix
/ \
| 1 2 |
a = | |
| 3 4 |
\ /
Elements of a matrix may be arbitrary expressions, provided that the dimensions all make sense when combining the various pieces. For example, given the above matrix, the expression
[ a, a ]
produces the matrix
ans = 1 2 1 2 3 4 3 4
but the expression
[ a, 1 ]
produces the error
error: number of rows must match near line 13, column 6
(assuming that this expression was entered as the first thing on line 13, of course).
Inside the square brackets that delimit a matrix expression, Octave looks at the surrounding context to determine whether spaces and newline characters should be converted into element and row separators, or simply ignored, so commands like
[ linspace (1, 2) ]
and
a = [ 1 2
3 4 ]
will work. However, some possible sources of confusion remain. For example, in the expression
[ 1 - 1 ]
the ‘-’ is treated as a binary operator and the result is the scalar 0, but in the expression
[ 1 -1 ]
the ‘-’ is treated as a unary operator and the result is the
vector [ 1, -1 ].
Given a = 1, the expression
[ 1 a' ]
results in the single quote character ‘'’ being treated as a
transpose operator and the result is the vector [ 1, 1 ], but the
expression
[ 1 a ' ]
produces the error message
error: unterminated string constant
because to not do so would make it impossible to correctly parse the valid expression
[ a 'foo' ]
For clarity, it is probably best to always use commas and semicolons to
separate matrix elements and rows. It is possible to enforce this style
by setting the built-in variable whitespace_in_literal_matrix to
"ignore".
- Built-in Variable: whitespace_in_literal_matrix
- This variable allows some control over how Octave decides to convert
spaces to commas and semicolons in matrix expressions like
[m (1)]or[ 1, 2, 3, 4 ]
If the value of
whitespace_in_literal_matrixis"ignore", Octave will never insert a comma or a semicolon in a literal matrix list. For example, the expression[1 2]will result in an error instead of being treated the same as[1, 2], and the expression[ 1, 2, 3, 4 ]
will result in the vector
[ 1, 2, 3, 4 ]instead of a matrix.If the value of
whitespace_in_literal_matrixis"traditional", Octave will convert spaces to a comma between identifiers and ‘(’. For example, given the matrixm = [3 2]
the expression
[m (1)]
will be parsed as
[m, (1)]
and will result in
[3 2 1]
and the expression
[ 1, 2, 3, 4 ]
will result in a matrix because the newline character is converted to a semicolon (row separator) even though there is a comma at the end of the first line (trailing commas or semicolons are ignored). This is apparently how MATLAB behaves.
Any other value for
whitespace_in_literal_matrixresults in behavior that is the same as traditional, except that Octave does not convert spaces to a comma between identifiers and ‘(’. For example, the expression[m (1)]
will produce ‘3’. This is the way Octave has always behaved.
When you type a matrix or the name of a variable whose value is a matrix, Octave responds by printing the matrix in with neatly aligned rows and columns. If the rows of the matrix are too large to fit on the screen, Octave splits the matrix and displays a header before each section to indicate which columns are being displayed. You can use the following variables to control the format of the output.
- Built-in Variable: output_max_field_width
- This variable specifies the maximum width of a numeric output field. The default value is 10.
- Built-in Variable: output_precision
- This variable specifies the minimum number of significant figures to display for numeric output. The default value is 5.
It is possible to achieve a wide range of output styles by using
different values of output_precision and
output_max_field_width. Reasonable combinations can be set using
the format function. See section 13.1 Basic Input and Output.
- Built-in Variable: split_long_rows
- For large matrices, Octave may not be able to display all the columns of
a given row on one line of your screen. This can result in missing
information or output that is nearly impossible to decipher, depending
on whether your terminal truncates or wraps long lines.
If the value of
split_long_rowsis nonzero, Octave will display the matrix in a series of smaller pieces, each of which can fit within the limits of your terminal width. Each set of rows is labeled so that you can easily see which columns are currently being displayed. For example:octave:13> rand (2,10) ans = Columns 1 through 6: 0.75883 0.93290 0.40064 0.43818 0.94958 0.16467 0.75697 0.51942 0.40031 0.61784 0.92309 0.40201 Columns 7 through 10: 0.90174 0.11854 0.72313 0.73326 0.44672 0.94303 0.56564 0.82150
The default value of
split_long_rowsis nonzero.
Octave automatically switches to scientific notation when values become
very large or very small. This guarantees that you will see several
significant figures for every value in a matrix. If you would prefer to
see all values in a matrix printed in a fixed point format, you can set
the built-in variable fixed_point_format to a nonzero value. But
doing so is not recommended, because it can produce output that can
easily be misinterpreted.
- Built-in Variable: fixed_point_format
- If the value of this variable is nonzero, Octave will scale all values
in a matrix so that the largest may be written with one leading digit.
The scaling factor is printed on the first line of output. For example,
octave:1> logspace (1, 7, 5)' ans = 1.0e+07 * 0.00000 0.00003 0.00100 0.03162 1.00000
Notice that first value appears to be zero when it is actually 1. For this reason, you should be careful when setting
fixed_point_formatto a nonzero value.The default value of
fixed_point_formatis 0.
| ISBN 0954161726 | GNU Octave Manual | See the print edition |