/usr/share/doc/octave/octave.html/Defining-Indexing-And-Indexed-Assignment.html is in octave-doc 4.2.2-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 | <!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>Defining Indexing And Indexed Assignment (GNU Octave)</title>
<meta name="description" content="Defining Indexing And Indexed Assignment (GNU Octave)">
<meta name="keywords" content="Defining Indexing And Indexed Assignment (GNU Octave)">
<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="Indexing-Objects.html#Indexing-Objects" rel="up" title="Indexing Objects">
<link href="Indexed-Assignment-Optimization.html#Indexed-Assignment-Optimization" rel="next" title="Indexed Assignment Optimization">
<link href="Indexing-Objects.html#Indexing-Objects" rel="prev" title="Indexing Objects">
<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="Defining-Indexing-And-Indexed-Assignment"></a>
<div class="header">
<p>
Next: <a href="Indexed-Assignment-Optimization.html#Indexed-Assignment-Optimization" accesskey="n" rel="next">Indexed Assignment Optimization</a>, Up: <a href="Indexing-Objects.html#Indexing-Objects" accesskey="u" rel="up">Indexing Objects</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="Defining-Indexing-And-Indexed-Assignment-1"></a>
<h4 class="subsection">34.3.1 Defining Indexing And Indexed Assignment</h4>
<p>Objects can be indexed with parentheses or braces, either like
<code><var>obj</var>(<var>idx</var>)</code> or like <code><var>obj</var>{<var>idx</var>}</code>, or even
like <code><var>obj</var>(<var>idx</var>).<var>field</var></code>. However, it is up to the
programmer to decide what this indexing actually means. In the case of the
polynomial class <code><var>p</var>(<var>n</var>)</code> might mean either the coefficient of
the <var>n</var>-th power of the polynomial, or it might be the evaluation of the
polynomial at <var>n</var>. The meaning of this subscripted referencing is
determined by the <code>subsref</code> method.
</p>
<a name="XREFsubsref"></a><dl>
<dt><a name="index-subsref"></a>: <em></em> <strong>subsref</strong> <em>(<var>val</var>, <var>idx</var>)</em></dt>
<dd><p>Perform the subscripted element selection operation on <var>val</var> according
to the subscript specified by <var>idx</var>.
</p>
<p>The subscript <var>idx</var> must be a structure array with fields ‘<samp>type</samp>’
and ‘<samp>subs</samp>’. Valid values for ‘<samp>type</samp>’ are <code>"()"</code>,
<code>"{}"</code>, and <code>"."</code>. The ‘<samp>subs</samp>’ field may be either
<code>":"</code> or a cell array of index values.
</p>
<p>The following example shows how to extract the first two columns of a matrix
</p>
<div class="example">
<pre class="example">val = magic (3)
⇒ val = [ 8 1 6
3 5 7
4 9 2 ]
idx.type = "()";
idx.subs = {":", 1:2};
subsref (val, idx)
⇒ [ 8 1
3 5
4 9 ]
</pre></div>
<p>Note that this is the same as writing <code>val(:, 1:2)</code>.
</p>
<p>If <var>idx</var> is an empty structure array with fields ‘<samp>type</samp>’ and
‘<samp>subs</samp>’, return <var>val</var>.
</p>
<p><strong>See also:</strong> <a href="#XREFsubsasgn">subsasgn</a>, <a href="Manipulating-Structures.html#XREFsubstruct">substruct</a>.
</p></dd></dl>
<p>For example, this class uses the convention that indexing with <code>"()"</code>
evaluates the polynomial and indexing with <code>"{}"</code> returns the
<var>n</var>-th coefficient (of the <var>n</var>-th power). The code for the
<code>subsref</code> method looks like
</p>
<div class="example">
<pre class="verbatim">function r = subsref (p, s)
if (isempty (s))
error ("@polynomial/subsref: missing index");
endif
switch (s(1).type)
case "()"
idx = s(1).subs;
if (numel (idx) != 1)
error ("@polynomial/subsref: need exactly one index");
endif
r = polyval (fliplr (p.poly), idx{1});
case "{}"
idx = s(1).subs;
if (numel (idx) != 1)
error ("@polynomial/subsref: need exactly one index");
endif
if (isnumeric (idx{1}))
r = p.poly(idx{1}+1);
else
r = p.poly(idx{1});
endif
case "."
fld = s.subs;
if (! strcmp (fld, "poly"))
error ('@polynomial/subsref: invalid property "%s"', fld);
endif
r = p.poly;
otherwise
error ("@polynomial/subsref: invalid subscript type");
endswitch
if (numel (s) > 1)
r = subsref (r, s(2:end));
endif
endfunction
</pre></div>
<p>The equivalent functionality for subscripted assignments uses the
<code>subsasgn</code> method.
</p>
<a name="XREFsubsasgn"></a><dl>
<dt><a name="index-subsasgn"></a>: <em></em> <strong>subsasgn</strong> <em>(<var>val</var>, <var>idx</var>, <var>rhs</var>)</em></dt>
<dd><p>Perform the subscripted assignment operation according to the subscript
specified by <var>idx</var>.
</p>
<p>The subscript <var>idx</var> must be a structure array with fields ‘<samp>type</samp>’
and ‘<samp>subs</samp>’. Valid values for ‘<samp>type</samp>’ are <code>"()"</code>,
<code>"{}"</code>, and <code>"."</code>. The ‘<samp>subs</samp>’ field may be either
<code>":"</code> or a cell array of index values.
</p>
<p>The following example shows how to set the two first columns of a 3-by-3
matrix to zero.
</p>
<div class="example">
<pre class="example">val = magic (3);
idx.type = "()";
idx.subs = {":", 1:2};
subsasgn (val, idx, 0)
⇒ [ 0 0 6
0 0 7
0 0 2 ]
</pre></div>
<p>Note that this is the same as writing <code>val(:, 1:2) = 0</code>.
</p>
<p>If <var>idx</var> is an empty structure array with fields ‘<samp>type</samp>’ and
‘<samp>subs</samp>’, return <var>rhs</var>.
</p>
<p><strong>See also:</strong> <a href="#XREFsubsref">subsref</a>, <a href="Manipulating-Structures.html#XREFsubstruct">substruct</a>, <a href="#XREFoptimize_005fsubsasgn_005fcalls">optimize_subsasgn_calls</a>.
</p></dd></dl>
<a name="XREFoptimize_005fsubsasgn_005fcalls"></a><dl>
<dt><a name="index-optimize_005fsubsasgn_005fcalls"></a>: <em><var>val</var> =</em> <strong>optimize_subsasgn_calls</strong> <em>()</em></dt>
<dt><a name="index-optimize_005fsubsasgn_005fcalls-1"></a>: <em><var>old_val</var> =</em> <strong>optimize_subsasgn_calls</strong> <em>(<var>new_val</var>)</em></dt>
<dt><a name="index-optimize_005fsubsasgn_005fcalls-2"></a>: <em></em> <strong>optimize_subsasgn_calls</strong> <em>(<var>new_val</var>, "local")</em></dt>
<dd><p>Query or set the internal flag for <code>subsasgn</code> method call
optimizations.
</p>
<p>If true, Octave will attempt to eliminate the redundant copying when calling
the <code>subsasgn</code> method of a user-defined class.
</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="#XREFsubsasgn">subsasgn</a>.
</p></dd></dl>
<p>Note that the <code>subsref</code> and <code>subsasgn</code> methods always receive the
whole index chain, while they usually handle only the first element. It is the
responsibility of these methods to handle the rest of the chain (if needed),
usually by forwarding it again to <code>subsref</code> or <code>subsasgn</code>.
</p>
<p>If you wish to use the <code>end</code> keyword in subscripted expressions of an
object, then there must be an <code>end</code> method defined. For example, the
<code>end</code> method for the polynomial class might look like
</p>
<div class="example">
<pre class="verbatim">function r = end (obj, index_pos, num_indices)
if (num_indices != 1)
error ("polynomial object may only have one index");
endif
r = length (obj.poly) - 1;
endfunction
</pre></div>
<p>which is a fairly generic <code>end</code> method that has a behavior similar to the
<code>end</code> keyword for Octave Array classes. An example using the polynomial
class is then
</p>
<div class="example">
<pre class="example">p = polynomial ([1,2,3,4]);
p{end-1}
⇒ 3
</pre></div>
<p>Objects can also be used themselves as the index in a subscripted expression
and this is controlled by the <code>subsindex</code> function.
</p>
<a name="XREFsubsindex"></a><dl>
<dt><a name="index-subsindex"></a>: <em><var>idx</var> =</em> <strong>subsindex</strong> <em>(<var>obj</var>)</em></dt>
<dd><p>Convert an object to an index vector.
</p>
<p>When <var>obj</var> is a class object defined with a class constructor, then
<code>subsindex</code> is the overloading method that allows the conversion of
this class object to a valid indexing vector. It is important to note that
<code>subsindex</code> must return a zero-based real integer vector of the class
<code>"double"</code>. For example, if the class constructor were
</p>
<div class="example">
<pre class="example">function obj = myclass (a)
obj = class (struct ("a", a), "myclass");
endfunction
</pre></div>
<p>then the <code>subsindex</code> function
</p>
<div class="example">
<pre class="example">function idx = subsindex (obj)
idx = double (obj.a) - 1.0;
endfunction
</pre></div>
<p>could be used as follows
</p>
<div class="example">
<pre class="example">a = myclass (1:4);
b = 1:10;
b(a)
⇒ 1 2 3 4
</pre></div>
<p><strong>See also:</strong> <a href="Built_002din-Data-Types.html#XREFclass">class</a>, <a href="#XREFsubsref">subsref</a>, <a href="#XREFsubsasgn">subsasgn</a>.
</p></dd></dl>
<p>Finally, objects can be used like ranges by providing a <code>colon</code> method.
</p>
<a name="XREFcolon"></a><dl>
<dt><a name="index-colon"></a>: <em><var>r</var> =</em> <strong>colon</strong> <em>(<var>base</var>, <var>limit</var>)</em></dt>
<dt><a name="index-colon-1"></a>: <em><var>r</var> =</em> <strong>colon</strong> <em>(<var>base</var>, <var>increment</var>, <var>limit</var>)</em></dt>
<dd><p>Return the result of the colon expression corresponding to <var>base</var>,
<var>limit</var>, and optionally, <var>increment</var>.
</p>
<p>This function is equivalent to the operator syntax
<code><var>base</var> : <var>limit</var></code><!-- /@w --> or
<code><var>base</var> : <var>increment</var> : <var>limit</var></code><!-- /@w -->.
</p>
<p><strong>See also:</strong> <a href="Special-Utility-Matrices.html#XREFlinspace">linspace</a>.
</p></dd></dl>
<hr>
<div class="header">
<p>
Next: <a href="Indexed-Assignment-Optimization.html#Indexed-Assignment-Optimization" accesskey="n" rel="next">Indexed Assignment Optimization</a>, Up: <a href="Indexing-Objects.html#Indexing-Objects" accesskey="u" rel="up">Indexing Objects</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>
|