<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <!-- Created by GNU Texinfo 6.5, http://www.gnu.org/software/texinfo/ --> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Matrices (GNU Octave (version 5.1.0))</title> <meta name="description" content="Matrices (GNU Octave (version 5.1.0))"> <meta name="keywords" content="Matrices (GNU Octave (version 5.1.0))"> <meta name="resource-type" content="document"> <meta name="distribution" content="global"> <meta name="Generator" content="makeinfo"> <link href="index.html#Top" rel="start" title="Top"> <link href="Concept-Index.html#Concept-Index" rel="index" title="Concept Index"> <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents"> <link href="Numeric-Data-Types.html#Numeric-Data-Types" rel="up" title="Numeric Data Types"> <link href="Empty-Matrices.html#Empty-Matrices" rel="next" title="Empty Matrices"> <link href="Numeric-Data-Types.html#Numeric-Data-Types" rel="prev" title="Numeric Data Types"> <style type="text/css"> <!-- a.summary-letter {text-decoration: none} blockquote.indentedblock {margin-right: 0em} blockquote.smallindentedblock {margin-right: 0em; font-size: smaller} blockquote.smallquotation {font-size: smaller} div.display {margin-left: 3.2em} div.example {margin-left: 3.2em} div.lisp {margin-left: 3.2em} div.smalldisplay {margin-left: 3.2em} div.smallexample {margin-left: 3.2em} div.smalllisp {margin-left: 3.2em} kbd {font-style: oblique} pre.display {font-family: inherit} pre.format {font-family: inherit} pre.menu-comment {font-family: serif} pre.menu-preformatted {font-family: serif} pre.smalldisplay {font-family: inherit; font-size: smaller} pre.smallexample {font-size: smaller} pre.smallformat {font-family: inherit; font-size: smaller} pre.smalllisp {font-size: smaller} span.nolinebreak {white-space: nowrap} span.roman {font-family: initial; font-weight: normal} span.sansserif {font-family: sans-serif; font-weight: normal} ul.no-bullet {list-style: none} --> </style> <link rel="stylesheet" type="text/css" href="octave.css"> </head> <body lang="en"> <a name="Matrices"></a> <div class="header"> <p> Next: <a href="Ranges.html#Ranges" accesskey="n" rel="next">Ranges</a>, Up: <a href="Numeric-Data-Types.html#Numeric-Data-Types" accesskey="u" rel="up">Numeric Data Types</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p> </div> <hr> <a name="Matrices-1"></a> <h3 class="section">4.1 Matrices</h3> <a name="index-matrices"></a> <a name="index-_005b"></a> <a name="index-_005d"></a> <a name="index-_003b"></a> <a name="index-_002c"></a> <p>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 </p> <div class="example"> <pre class="example">a = [1, 2; 3, 4] </pre></div> <p>results in the matrix </p> <div class="example"> <pre class="example"> / \ | 1 2 | a = | | | 3 4 | \ / </pre></div> <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 </p> <div class="example"> <pre class="example">[ a, a ] </pre></div> <p>produces the matrix </p> <div class="example"> <pre class="example">ans = 1 2 1 2 3 4 3 4 </pre></div> <p>but the expression </p> <div class="example"> <pre class="example">[ a, 1 ] </pre></div> <p>produces the error </p> <div class="example"> <pre class="example">error: number of rows must match (1 != 2) near line 13, column 6 </pre></div> <p>(assuming that this expression was entered as the first thing on line 13, of course). </p> <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 </p> <div class="example"> <pre class="example">a = [ 1 2 3 4 ] </pre></div> <p>will work. However, some possible sources of confusion remain. For example, in the expression </p> <div class="example"> <pre class="example">[ 1 - 1 ] </pre></div> <p>the ‘<samp>-</samp>’ is treated as a binary operator and the result is the scalar 0, but in the expression </p> <div class="example"> <pre class="example">[ 1 -1 ] </pre></div> <p>the ‘<samp>-</samp>’ is treated as a unary operator and the result is the vector <code>[ 1, -1 ]</code>. Similarly, the expression </p> <div class="example"> <pre class="example">[ sin (pi) ] </pre></div> <p>will be parsed as </p> <div class="example"> <pre class="example">[ sin, (pi) ] </pre></div> <p>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: </p> <div class="example"> <pre class="example">[ (sin (pi)) ] </pre></div> <p>Whitespace surrounding the single quote character (‘<samp>'</samp>’, used as a transpose operator and for delimiting character strings) can also cause confusion. Given <code>a = 1</code>, the expression </p> <div class="example"> <pre class="example">[ 1 a' ] </pre></div> <p>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 </p> <div class="example"> <pre class="example">[ 1 a ' ] </pre></div> <p>produces the error message </p> <div class="example"> <pre class="example">parse error: syntax error >>> [ 1 a ' ] ^ </pre></div> <p>because not doing so would cause trouble when parsing the valid expression </p> <div class="example"> <pre class="example">[ a 'foo' ] </pre></div> <p>For clarity, it is probably best to always use commas and semicolons to separate matrix elements and rows. </p> <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. </p> <a name="XREFsizemax"></a><dl> <dt><a name="index-sizemax"></a><em></em> <strong>sizemax</strong> <em>()</em></dt> <dd><p>Return the largest value allowed for the size of an array. </p> <p>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>. </p> <p><strong>See also:</strong> <a href="Integer-Data-Types.html#XREFintmax">intmax</a>. </p></dd></dl> <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. </p> <a name="XREFoutput_005fprecision"></a><dl> <dt><a name="index-output_005fprecision"></a><em><var>val</var> =</em> <strong>output_precision</strong> <em>()</em></dt> <dt><a name="index-output_005fprecision-1"></a><em><var>old_val</var> =</em> <strong>output_precision</strong> <em>(<var>new_val</var>)</em></dt> <dt><a name="index-output_005fprecision-2"></a><em></em> <strong>output_precision</strong> <em>(<var>new_val</var>, "local")</em></dt> <dd><p>Query or set the internal variable that specifies the minimum number of significant figures to display for numeric output. </p> <p>Note that regardless of the value set for <code>output_precision</code>, the number of digits of precision displayed is limited to 16 for double precision values and 7 for single precision values. </p> <p>When called from inside a function with the <code>"local"</code> option, the variable is changed locally for the function and any subroutines it calls. The original variable value is restored when exiting the function. </p> <p><strong>See also:</strong> <a href="Terminal-Output.html#XREFformat">format</a>, <a href="#XREFfixed_005fpoint_005fformat">fixed_point_format</a>. </p></dd></dl> <p>It is possible to achieve a wide range of output styles by using different values of <code>output_precision</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>. </p> <a name="XREFsplit_005flong_005frows"></a><dl> <dt><a name="index-split_005flong_005frows"></a><em><var>val</var> =</em> <strong>split_long_rows</strong> <em>()</em></dt> <dt><a name="index-split_005flong_005frows-1"></a><em><var>old_val</var> =</em> <strong>split_long_rows</strong> <em>(<var>new_val</var>)</em></dt> <dt><a name="index-split_005flong_005frows-2"></a><em></em> <strong>split_long_rows</strong> <em>(<var>new_val</var>, "local")</em></dt> <dd><p>Query or set the internal variable that controls whether rows of a matrix may be split when displayed to a terminal window. </p> <p>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: </p> <div class="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></div> <p>When called from inside a function with the <code>"local"</code> option, the variable is changed locally for the function and any subroutines it calls. The original variable value is restored when exiting the function. </p> <p><strong>See also:</strong> <a href="Terminal-Output.html#XREFformat">format</a>. </p></dd></dl> <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. </p> <a name="XREFfixed_005fpoint_005fformat"></a><dl> <dt><a name="index-fixed_005fpoint_005fformat"></a><em><var>val</var> =</em> <strong>fixed_point_format</strong> <em>()</em></dt> <dt><a name="index-fixed_005fpoint_005fformat-1"></a><em><var>old_val</var> =</em> <strong>fixed_point_format</strong> <em>(<var>new_val</var>)</em></dt> <dt><a name="index-fixed_005fpoint_005fformat-2"></a><em></em> <strong>fixed_point_format</strong> <em>(<var>new_val</var>, "local")</em></dt> <dd><p>Query or set the internal variable that controls whether Octave will use a scaled format to print matrix values. </p> <p>The scaled format prints a scaling factor on the first line of output chosen such that the largest matrix element can be written with a single leading digit. For example: </p> <div class="example"> <pre class="example">fixed_point_format (true) logspace (1, 7, 5)' ans = 1.0e+07 * 0.00000 0.00003 0.00100 0.03162 1.00000 </pre></div> <p>Notice that the first value appears to be 0 when it is actually 1. Because of the possibility for confusion you should be careful about enabling <code>fixed_point_format</code>. </p> <p>When called from inside a function with the <code>"local"</code> option, the variable is changed locally for the function and any subroutines it calls. The original variable value is restored when exiting the function. </p> <p><strong>See also:</strong> <a href="Terminal-Output.html#XREFformat">format</a>, <a href="#XREFoutput_005fprecision">output_precision</a>. </p></dd></dl> <table class="menu" border="0" cellspacing="0"> <tr><td align="left" valign="top">• <a href="Empty-Matrices.html#Empty-Matrices" accesskey="1">Empty Matrices</a>:</td><td> </td><td align="left" valign="top"> </td></tr> </table> <hr> <div class="header"> <p> Next: <a href="Ranges.html#Ranges" accesskey="n" rel="next">Ranges</a>, Up: <a href="Numeric-Data-Types.html#Numeric-Data-Types" accesskey="u" rel="up">Numeric Data Types</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p> </div> </body> </html>