1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- Created by GNU Texinfo 6.7, http://www.gnu.org/software/texinfo/ -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Object Sizes (GNU Octave (version 6.2.0))</title>
<meta name="description" content="Object Sizes (GNU Octave (version 6.2.0))">
<meta name="keywords" content="Object Sizes (GNU Octave (version 6.2.0))">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<link href="index.html" rel="start" title="Top">
<link href="Concept-Index.html" rel="index" title="Concept Index">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="Data-Types.html" rel="up" title="Data Types">
<link href="Numeric-Data-Types.html" rel="next" title="Numeric Data Types">
<link href="User_002ddefined-Data-Types.html" rel="prev" title="User-defined Data Types">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.indentedblock {margin-right: 0em}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.lisp {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}
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">
<span id="Object-Sizes"></span><div class="header">
<p>
Previous: <a href="User_002ddefined-Data-Types.html" accesskey="p" rel="prev">User-defined Data Types</a>, Up: <a href="Data-Types.html" accesskey="u" rel="up">Data Types</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<span id="Object-Sizes-1"></span><h3 class="section">3.3 Object Sizes</h3>
<p>The following functions allow you to determine the size of a variable or
expression. These functions are defined for all objects. They return
-1 when the operation doesn’t make sense. For example, Octave’s
data structure type doesn’t have rows or columns, so the <code>rows</code> and
<code>columns</code> functions return -1 for structure arguments.
</p>
<span id="XREFndims"></span><dl>
<dt id="index-ndims">: <em></em> <strong>ndims</strong> <em>(<var>a</var>)</em></dt>
<dd><p>Return the number of dimensions of <var>a</var>.
</p>
<p>For any array, the result will always be greater than or equal to 2.
Trailing singleton dimensions are not counted, i.e., tailing dimensions <var>d</var>
greater than 2, for which <code>size (<var>a</var>, <var>d</var>) = 1</code>.
</p>
<div class="example">
<pre class="example">ndims (ones (4, 1, 2, 1))
⇒ 3
</pre></div>
<p><strong>See also:</strong> <a href="#XREFsize">size</a>.
</p></dd></dl>
<span id="XREFcolumns"></span><dl>
<dt id="index-columns">: <em></em> <strong>columns</strong> <em>(<var>a</var>)</em></dt>
<dd><p>Return the number of columns of <var>a</var>. This is equivalent to
<code>size (<var>a</var>, 2)</code>.
</p>
<p><strong>See also:</strong> <a href="#XREFrows">rows</a>, <a href="#XREFsize">size</a>, <a href="#XREFlength">length</a>, <a href="#XREFnumel">numel</a>, <a href="Predicates-for-Numeric-Objects.html#XREFisscalar">isscalar</a>, <a href="Predicates-for-Numeric-Objects.html#XREFisvector">isvector</a>, <a href="Predicates-for-Numeric-Objects.html#XREFismatrix">ismatrix</a>.
</p></dd></dl>
<span id="XREFrows"></span><dl>
<dt id="index-rows">: <em></em> <strong>rows</strong> <em>(<var>a</var>)</em></dt>
<dd><p>Return the number of rows of <var>a</var>. This is equivalent to
<code>size (<var>a</var>, 1)</code>.
</p>
<p><strong>See also:</strong> <a href="#XREFcolumns">columns</a>, <a href="#XREFsize">size</a>, <a href="#XREFlength">length</a>, <a href="#XREFnumel">numel</a>, <a href="Predicates-for-Numeric-Objects.html#XREFisscalar">isscalar</a>, <a href="Predicates-for-Numeric-Objects.html#XREFisvector">isvector</a>, <a href="Predicates-for-Numeric-Objects.html#XREFismatrix">ismatrix</a>.
</p></dd></dl>
<span id="XREFnumel"></span><dl>
<dt id="index-numel">: <em></em> <strong>numel</strong> <em>(<var>a</var>)</em></dt>
<dt id="index-numel-1">: <em></em> <strong>numel</strong> <em>(<var>a</var>, <var>idx1</var>, <var>idx2</var>, …)</em></dt>
<dd><p>Return the number of elements in the object <var>a</var>.
</p>
<p>Optionally, if indices <var>idx1</var>, <var>idx2</var>, … are supplied,
return the number of elements that would result from the indexing
</p>
<div class="example">
<pre class="example"><var>a</var>(<var>idx1</var>, <var>idx2</var>, …)
</pre></div>
<p>Note that the indices do not have to be scalar numbers. For example,
</p>
<div class="example">
<pre class="example"><var>a</var> = 1;
<var>b</var> = ones (2, 3);
numel (<var>a</var>, <var>b</var>)
</pre></div>
<p>will return 6, as this is the number of ways to index with <var>b</var>.
Or the index could be the string <code>":"</code> which represents the colon
operator. For example,
</p>
<div class="example">
<pre class="example"><var>a</var> = ones (5, 3);
numel (<var>a</var>, 2, ":")
</pre></div>
<p>will return 3 as the second row has three column entries.
</p>
<p>This method is also called when an object appears as lvalue with cs-list
indexing, i.e., <code>object{…}</code> or <code>object(…).field</code>.
</p>
<p><strong>See also:</strong> <a href="#XREFsize">size</a>, <a href="#XREFlength">length</a>, <a href="#XREFndims">ndims</a>.
</p></dd></dl>
<span id="XREFlength"></span><dl>
<dt id="index-length">: <em></em> <strong>length</strong> <em>(<var>a</var>)</em></dt>
<dd><p>Return the length of the object <var>a</var>.
</p>
<p>The length is 0 for empty objects, 1 for scalars, and the number of elements
for vectors. For matrix or N-dimensional objects, the length is the number
of elements along the largest dimension
(equivalent to <code>max (size (<var>a</var>))</code><!-- /@w -->).
</p>
<p><strong>See also:</strong> <a href="#XREFnumel">numel</a>, <a href="#XREFsize">size</a>.
</p></dd></dl>
<span id="XREFsize"></span><dl>
<dt id="index-size">: <em><var>sz</var> =</em> <strong>size</strong> <em>(<var>a</var>)</em></dt>
<dt id="index-size-1">: <em><var>dim_sz</var> =</em> <strong>size</strong> <em>(<var>a</var>, <var>dim</var>)</em></dt>
<dt id="index-size-2">: <em>[<var>rows</var>, <var>cols</var>, …, <var>dim_N_sz</var>] =</em> <strong>size</strong> <em>(…)</em></dt>
<dd><p>Return a row vector with the size (number of elements) of each dimension for
the object <var>a</var>.
</p>
<p>When given a second argument, <var>dim</var>, return the size of the corresponding
dimension.
</p>
<p>With a single output argument, <code>size</code> returns a row vector. When called
with multiple output arguments, <code>size</code> returns the size of dimension N
in the Nth argument. The number of rows, dimension 1, is returned in the
first argument, the number of columns, dimension 2, is returned in the
second argument, etc. If there are more dimensions in <var>a</var> than there are
output arguments, <code>size</code> returns the total number of elements in the
remaining dimensions in the final output argument.
</p>
<p>Example 1: single row vector output
</p>
<div class="example">
<pre class="example">size ([1, 2; 3, 4; 5, 6])
⇒ [ 3, 2 ]
</pre></div>
<p>Example 2: number of elements in 2nd dimension (columns)
</p>
<div class="example">
<pre class="example">size ([1, 2; 3, 4; 5, 6], 2)
⇒ 2
</pre></div>
<p>Example 3: number of output arguments == number of dimensions
</p>
<div class="example">
<pre class="example">[nr, nc] = size ([1, 2; 3, 4; 5, 6])
⇒ nr = 3
⇒ nc = 2
</pre></div>
<p>Example 4: number of output arguments < number of dimensions
</p>
<div class="example">
<pre class="example">[nr, remainder] = size (ones (2, 3, 4, 5))
⇒ nr = 2
⇒ remainder = 60
</pre></div>
<p><strong>See also:</strong> <a href="#XREFnumel">numel</a>, <a href="#XREFndims">ndims</a>, <a href="#XREFlength">length</a>, <a href="#XREFrows">rows</a>, <a href="#XREFcolumns">columns</a>, <a href="#XREFsize_005fequal">size_equal</a>, <a href="Finding-Elements-and-Checking-Conditions.html#XREFcommon_005fsize">common_size</a>.
</p></dd></dl>
<span id="XREFisempty"></span><dl>
<dt id="index-isempty">: <em></em> <strong>isempty</strong> <em>(<var>a</var>)</em></dt>
<dd><p>Return true if <var>a</var> is an empty matrix (any one of its dimensions is
zero).
</p>
<p><strong>See also:</strong> <a href="#XREFisnull">isnull</a>, <a href="Built_002din-Data-Types.html#XREFisa">isa</a>.
</p></dd></dl>
<span id="XREFisnull"></span><dl>
<dt id="index-isnull">: <em></em> <strong>isnull</strong> <em>(<var>x</var>)</em></dt>
<dd><p>Return true if <var>x</var> is a special null matrix, string, or single quoted
string.
</p>
<p>Indexed assignment with such a null value on the right-hand side should delete
array elements. This function is used in place of <code>isempty</code> when
overloading the indexed assignment method (<code>subsasgn</code>) for user-defined
classes. <code>isnull</code> is used to distinguish between these two cases:
</p>
<p><code><var>A</var>(<var>I</var>) = []</code>
</p>
<p>and
</p>
<p><code><var>X</var> = []; <var>A</var>(<var>I</var>) = <var>X</var></code>
</p>
<p>In the first assignment, the right-hand side is <code>[]</code> which is a special
null value. As long as the index <var>I</var> is not empty, this code should
delete elements from <var>A</var> rather than perform assignment.
</p>
<p>In the second assignment, the right-hand side is empty (because <var>X</var> is
<code>[]</code>), but it is <strong>not</strong> null. This code should assign the empty
value to elements in <var>A</var>.
</p>
<p>An example from Octave’s built-in char class demonstrates the interpreter
behavior when <code>isnull</code> is used correctly.
</p>
<div class="example">
<pre class="example">str = "Hello World";
nm = "Wally";
str(7:end) = nm # indexed assignment
⇒ str = Hello Wally
str(7:end) = "" # indexed deletion
⇒ str = Hello
</pre></div>
<p><strong>See also:</strong> <a href="#XREFisempty">isempty</a>, <a href="Advanced-Indexing.html#XREFisindex">isindex</a>.
</p></dd></dl>
<span id="XREFsizeof"></span><dl>
<dt id="index-sizeof">: <em></em> <strong>sizeof</strong> <em>(<var>val</var>)</em></dt>
<dd><p>Return the size of <var>val</var> in bytes.
</p>
<p><strong>See also:</strong> <a href="Status-of-Variables.html#XREFwhos">whos</a>.
</p></dd></dl>
<span id="XREFsize_005fequal"></span><dl>
<dt id="index-size_005fequal">: <em></em> <strong>size_equal</strong> <em>(<var>a</var>, <var>b</var>, …)</em></dt>
<dd><p>Return true if the dimensions of all arguments agree.
</p>
<p>Trailing singleton dimensions are ignored. When called with a single argument,
or no argument, <code>size_equal</code> returns true.
</p>
<p><strong>See also:</strong> <a href="#XREFsize">size</a>, <a href="#XREFnumel">numel</a>, <a href="#XREFndims">ndims</a>, <a href="Finding-Elements-and-Checking-Conditions.html#XREFcommon_005fsize">common_size</a>.
</p></dd></dl>
<span id="XREFsqueeze"></span><dl>
<dt id="index-squeeze">: <em></em> <strong>squeeze</strong> <em>(<var>x</var>)</em></dt>
<dd><p>Remove singleton dimensions from <var>x</var> and return the result.
</p>
<p>Note that for compatibility with <small>MATLAB</small>, all objects have
a minimum of two dimensions and row vectors are left unchanged.
</p>
<p><strong>See also:</strong> <a href="Rearranging-Matrices.html#XREFreshape">reshape</a>.
</p></dd></dl>
<hr>
<div class="header">
<p>
Previous: <a href="User_002ddefined-Data-Types.html" accesskey="p" rel="prev">User-defined Data Types</a>, Up: <a href="Data-Types.html" accesskey="u" rel="up">Data Types</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html" title="Index" rel="index">Index</a>]</p>
</div>
</body>
</html>
|