<html lang="en"> <head> <title>Matrices - GNU Octave</title> <meta http-equiv="Content-Type" content="text/html"> <meta name="description" content="GNU Octave"> <meta name="generator" content="makeinfo 4.13"> <link title="Top" rel="start" href="index.html#Top"> <link rel="up" href="Numeric-Data-Types.html#Numeric-Data-Types" title="Numeric Data Types"> <link rel="next" href="Ranges.html#Ranges" title="Ranges"> <link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> <meta http-equiv="Content-Style-Type" content="text/css"> <style type="text/css"><!-- pre.display { font-family:inherit } pre.format { font-family:inherit } pre.smalldisplay { font-family:inherit; font-size:smaller } pre.smallformat { font-family:inherit; font-size:smaller } pre.smallexample { font-size:smaller } pre.smalllisp { font-size:smaller } span.sc { font-variant:small-caps } span.roman { font-family:serif; font-weight:normal; } span.sansserif { font-family:sans-serif; font-weight:normal; } --></style> </head> <body> <div class="node"> <a name="Matrices"></a> <p> Next: <a rel="next" accesskey="n" href="Ranges.html#Ranges">Ranges</a>, Up: <a rel="up" accesskey="u" href="Numeric-Data-Types.html#Numeric-Data-Types">Numeric Data Types</a> <hr> </div> <h3 class="section">4.1 Matrices</h3> <p><a name="index-matrices-246"></a> <a name="index-g_t_005b-247"></a><a name="index-g_t_005d-248"></a><a name="index-g_t_003b-249"></a><a name="index-g_t_002c-250"></a> 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 <pre class="example"> a = [1, 2; 3, 4] </pre> <p class="noindent">results in the matrix <pre class="example"> / \ | 1 2 | a = | | | 3 4 | \ / </pre> <p>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 <pre class="example"> [ a, a ] </pre> <p class="noindent">produces the matrix <pre class="example"> ans = 1 2 1 2 3 4 3 4 </pre> <p class="noindent">but the expression <pre class="example"> [ a, 1 ] </pre> <p class="noindent">produces the error <pre class="example"> error: number of rows must match (1 != 2) near line 13, column 6 </pre> <p class="noindent">(assuming that this expression was entered as the first thing on line 13, of course). <p>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 an expression like <pre class="example"> a = [ 1 2 3 4 ] </pre> <p class="noindent">will work. However, some possible sources of confusion remain. For example, in the expression <pre class="example"> [ 1 - 1 ] </pre> <p class="noindent">the ‘<samp><span class="samp">-</span></samp>’ is treated as a binary operator and the result is the scalar 0, but in the expression <pre class="example"> [ 1 -1 ] </pre> <p class="noindent">the ‘<samp><span class="samp">-</span></samp>’ is treated as a unary operator and the result is the vector <code>[ 1, -1 ]</code>. Similarly, the expression <pre class="example"> [ sin (pi) ] </pre> <p class="noindent">will be parsed as <pre class="example"> [ sin, (pi) ] </pre> <p class="noindent">and will result in an error since the <code>sin</code> function will be called with no arguments. To get around this, you must omit the space between <code>sin</code> and the opening parenthesis, or enclose the expression in a set of parentheses: <pre class="example"> [ (sin (pi)) ] </pre> <p>Whitespace surrounding the single quote character (‘<samp><span class="samp">'</span></samp>’, used as a transpose operator and for delimiting character strings) can also cause confusion. Given <code>a = 1</code>, the expression <pre class="example"> [ 1 a' ] </pre> <p class="noindent">results in the single quote character being treated as a transpose operator and the result is the vector <code>[ 1, 1 ]</code>, but the expression <pre class="example"> [ 1 a ' ] </pre> <p class="noindent">produces the error message <pre class="example"> parse error: syntax error >>> [ 1 a ' ] ^ </pre> <p class="noindent">because not doing so would cause trouble when parsing the valid expression <pre class="example"> [ a 'foo' ] </pre> <p>For clarity, it is probably best to always use commas and semicolons to separate matrix elements and rows. <p>The maximum number of elements in a matrix is fixed when Octave is compiled. The allowable number can be queried with the function <code>sizemax</code>. Note that other factors, such as the amount of memory available on your machine, may limit the maximum size of matrices to something smaller. <!-- sizemax src/bitfcns.cc --> <p><a name="doc_002dsizemax"></a> <div class="defun"> — Built-in Function: <b>sizemax</b> ()<var><a name="index-sizemax-251"></a></var><br> <blockquote><p>Return the largest value allowed for the size of an array. If Octave is compiled with 64-bit indexing, the result is of class int64, otherwise it is of class int32. The maximum array size is slightly smaller than the maximum value allowable for the relevant class as reported by <code>intmax</code>. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dintmax.html#doc_002dintmax">intmax</a>. </p></blockquote></div> <p>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. <!-- output_max_field_width src/pr-output.cc --> <p><a name="doc_002doutput_005fmax_005ffield_005fwidth"></a> <div class="defun"> — Built-in Function: <var>val</var> = <b>output_max_field_width</b> ()<var><a name="index-output_005fmax_005ffield_005fwidth-252"></a></var><br> — Built-in Function: <var>old_val</var> = <b>output_max_field_width</b> (<var>new_val</var>)<var><a name="index-output_005fmax_005ffield_005fwidth-253"></a></var><br> — Built-in Function: <b>output_max_field_width</b> (<var>new_val, "local"</var>)<var><a name="index-output_005fmax_005ffield_005fwidth-254"></a></var><br> <blockquote><p>Query or set the internal variable that specifies the maximum width of a numeric output field. <p>When called from inside a function with the "local" option, the variable is changed locally for the function and any subroutines it calls. The original variable value is restored when exiting the function. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dformat.html#doc_002dformat">format</a>, <a href="doc_002dfixed_005fpoint_005fformat.html#doc_002dfixed_005fpoint_005fformat">fixed_point_format</a>, <a href="doc_002doutput_005fprecision.html#doc_002doutput_005fprecision">output_precision</a>. </p></blockquote></div> <!-- output_precision src/pr-output.cc --> <p><a name="doc_002doutput_005fprecision"></a> <div class="defun"> — Built-in Function: <var>val</var> = <b>output_precision</b> ()<var><a name="index-output_005fprecision-255"></a></var><br> — Built-in Function: <var>old_val</var> = <b>output_precision</b> (<var>new_val</var>)<var><a name="index-output_005fprecision-256"></a></var><br> — Built-in Function: <b>output_precision</b> (<var>new_val, "local"</var>)<var><a name="index-output_005fprecision-257"></a></var><br> <blockquote><p>Query or set the internal variable that specifies the minimum number of significant figures to display for numeric output. <p>When called from inside a function with the "local" option, the variable is changed locally for the function and any subroutines it calls. The original variable value is restored when exiting the function. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dformat.html#doc_002dformat">format</a>, <a href="doc_002dfixed_005fpoint_005fformat.html#doc_002dfixed_005fpoint_005fformat">fixed_point_format</a>, <a href="doc_002doutput_005fmax_005ffield_005fwidth.html#doc_002doutput_005fmax_005ffield_005fwidth">output_max_field_width</a>. </p></blockquote></div> <p>It is possible to achieve a wide range of output styles by using different values of <code>output_precision</code> and <code>output_max_field_width</code>. Reasonable combinations can be set using the <code>format</code> function. See <a href="Basic-Input-and-Output.html#Basic-Input-and-Output">Basic Input and Output</a>. <!-- split_long_rows src/pr-output.cc --> <p><a name="doc_002dsplit_005flong_005frows"></a> <div class="defun"> — Built-in Function: <var>val</var> = <b>split_long_rows</b> ()<var><a name="index-split_005flong_005frows-258"></a></var><br> — Built-in Function: <var>old_val</var> = <b>split_long_rows</b> (<var>new_val</var>)<var><a name="index-split_005flong_005frows-259"></a></var><br> — Built-in Function: <b>split_long_rows</b> (<var>new_val, "local"</var>)<var><a name="index-split_005flong_005frows-260"></a></var><br> <blockquote><p>Query or set the internal variable that controls whether rows of a matrix may be split when displayed to a terminal window. If the rows are split, Octave will display the matrix in a series of smaller pieces, each of which can fit within the limits of your terminal width and each set of rows is labeled so that you can easily see which columns are currently being displayed. For example: <pre class="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 </pre> <p>When called from inside a function with the "local" option, the variable is changed locally for the function and any subroutines it calls. The original variable value is restored when exiting the function. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dformat.html#doc_002dformat">format</a>. </p></blockquote></div> <p>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 <code>fixed_point_format</code> to a nonzero value. But doing so is not recommended, because it can produce output that can easily be misinterpreted. <!-- fixed_point_format src/pr-output.cc --> <p><a name="doc_002dfixed_005fpoint_005fformat"></a> <div class="defun"> — Built-in Function: <var>val</var> = <b>fixed_point_format</b> ()<var><a name="index-fixed_005fpoint_005fformat-261"></a></var><br> — Built-in Function: <var>old_val</var> = <b>fixed_point_format</b> (<var>new_val</var>)<var><a name="index-fixed_005fpoint_005fformat-262"></a></var><br> — Built-in Function: <b>fixed_point_format</b> (<var>new_val, "local"</var>)<var><a name="index-fixed_005fpoint_005fformat-263"></a></var><br> <blockquote><p>Query or set the internal variable that controls whether Octave will use a scaled format to print matrix values such that the largest element may be written with a single leading digit with the scaling factor is printed on the first line of output. For example: <pre class="example"> octave:1> logspace (1, 7, 5)' ans = 1.0e+07 * 0.00000 0.00003 0.00100 0.03162 1.00000 </pre> <p class="noindent">Notice that first value appears to be zero when it is actually 1. For this reason, you should be careful when setting <code>fixed_point_format</code> to a nonzero value. <p>When called from inside a function with the "local" option, the variable is changed locally for the function and any subroutines it calls. The original variable value is restored when exiting the function. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dformat.html#doc_002dformat">format</a>, <a href="doc_002doutput_005fmax_005ffield_005fwidth.html#doc_002doutput_005fmax_005ffield_005fwidth">output_max_field_width</a>, <a href="doc_002doutput_005fprecision.html#doc_002doutput_005fprecision">output_precision</a>. </p></blockquote></div> <ul class="menu"> <li><a accesskey="1" href="Empty-Matrices.html#Empty-Matrices">Empty Matrices</a> </ul> </body></html>