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
|
<!DOCTYPE html>
<html>
<!-- Created by GNU Texinfo 7.1.1, https://www.gnu.org/software/texinfo/ -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Indexing Cell Arrays (GNU Octave (version 10.3.0))</title>
<meta name="description" content="Indexing Cell Arrays (GNU Octave (version 10.3.0))">
<meta name="keywords" content="Indexing Cell Arrays (GNU Octave (version 10.3.0))">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<meta name="viewport" content="width=device-width,initial-scale=1">
<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="Cell-Arrays.html" rel="up" title="Cell Arrays">
<link href="Cell-Arrays-of-Strings.html" rel="next" title="Cell Arrays of Strings">
<link href="Creating-Cell-Arrays.html" rel="prev" title="Creating Cell Arrays">
<style type="text/css">
<!--
a.copiable-link {visibility: hidden; text-decoration: none; line-height: 0em}
div.example {margin-left: 3.2em}
span:hover a.copiable-link {visibility: visible}
strong.def-name {font-family: monospace; font-weight: bold; font-size: larger}
-->
</style>
<link rel="stylesheet" type="text/css" href="octave.css">
</head>
<body lang="en">
<div class="subsection-level-extent" id="Indexing-Cell-Arrays">
<div class="nav-panel">
<p>
Next: <a href="Cell-Arrays-of-Strings.html" accesskey="n" rel="next">Cell Arrays of Strings</a>, Previous: <a href="Creating-Cell-Arrays.html" accesskey="p" rel="prev">Creating Cell Arrays</a>, Up: <a href="Cell-Arrays.html" accesskey="u" rel="up">Cell Arrays</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>
<h4 class="subsection" id="Indexing-Cell-Arrays-1"><span>6.3.3 Indexing Cell Arrays<a class="copiable-link" href="#Indexing-Cell-Arrays-1"> ¶</a></span></h4>
<p>As shown in see <a class="pxref" href="Basic-Usage-of-Cell-Arrays.html">Basic Usage of Cell Arrays</a> elements can be
extracted from cell arrays using the ‘<samp class="samp">{</samp>’ and ‘<samp class="samp">}</samp>’
operators. If you want to extract or access subarrays which are still
cell arrays, you need to use the ‘<samp class="samp">(</samp>’ and ‘<samp class="samp">)</samp>’ operators. The
following example illustrates the difference:
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">c = {"1", "2", "3"; "x", "y", "z"; "4", "5", "6"};
c{2,3}
⇒ ans = z
c(2,3)
⇒ ans =
{
[1,1] = z
}
</pre></div></div>
<p>So with ‘<samp class="samp">{}</samp>’ you access elements of a cell
array, while with ‘<samp class="samp">()</samp>’ you access a sub array of a cell
array.
</p>
<p>Using the ‘<samp class="samp">(</samp>’ and ‘<samp class="samp">)</samp>’ operators, indexing works for cell
arrays like for multi-dimensional arrays. As an example, all the rows
of the first and third column of a cell array can be set to <code class="code">0</code>
with the following command:
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">c(:, [1, 3]) = {0}
⇒ =
{
[1,1] = 0
[2,1] = 0
[3,1] = 0
[1,2] = 2
[2,2] = y
[3,2] = 5
[1,3] = 0
[2,3] = 0
[3,3] = 0
}
</pre></div></div>
<p>Note, that the above can also be achieved like this:
</p>
<div class="example">
<pre class="example-preformatted">c(:, [1, 3]) = 0;
</pre></div>
<p>Here, the scalar ‘<samp class="samp">0</samp>’ is automatically promoted to
cell array ‘<samp class="samp">{0}</samp>’ and then assigned to the subarray of <code class="code">c</code>.
</p>
<p>To give another example for indexing cell arrays with ‘<samp class="samp">()</samp>’, you
can exchange the first and the second row of a cell array as in the
following command:
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">c = {1, 2, 3; 4, 5, 6};
c([1, 2], :) = c([2, 1], :)
⇒ =
{
[1,1] = 4
[2,1] = 1
[1,2] = 5
[2,2] = 2
[1,3] = 6
[2,3] = 3
}
</pre></div></div>
<p>Accessing multiple elements of a cell array with the ‘<samp class="samp">{</samp>’ and
‘<samp class="samp">}</samp>’ operators will result in a comma-separated list of all the
requested elements (see <a class="pxref" href="Comma_002dSeparated-Lists.html">Comma-Separated Lists</a>). Using the
‘<samp class="samp">{</samp>’ and ‘<samp class="samp">}</samp>’ operators the first two rows in the above
example can be swapped back like this:
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">[c{[1,2], :}] = deal (c{[2, 1], :})
⇒ =
{
[1,1] = 1
[2,1] = 4
[1,2] = 2
[2,2] = 5
[1,3] = 3
[2,3] = 6
}
</pre></div></div>
<p>As for struct arrays and numerical arrays, the empty matrix ‘<samp class="samp">[]</samp>’
can be used to delete elements from a cell array:
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">x = {"1", "2"; "3", "4"};
x(1, :) = []
⇒ x =
{
[1,1] = 3
[1,2] = 4
}
</pre></div></div>
<p>The following example shows how to just remove the contents of cell
array elements but not delete the space for them:
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">x = {"1", "2"; "3", "4"};
x(1, :) = {[]}
⇒ x =
{
[1,1] = [](0x0)
[2,1] = 3
[1,2] = [](0x0)
[2,2] = 4
}
</pre></div></div>
<p>The indexing operations operate on the cell array and not on the objects
within the cell array. By contrast, <code class="code">cellindexmat</code> applies matrix
indexing to the objects within each cell array entry and returns the requested
values.
</p>
<a class="anchor" id="XREFcellindexmat"></a><span style="display:block; margin-top:-4.5ex;"> </span>
<dl class="first-deftypefn">
<dt class="deftypefn" id="index-cellindexmat"><span><code class="def-type"><var class="var">y</var> =</code> <strong class="def-name">cellindexmat</strong> <code class="def-code-arguments">(<var class="var">x</var>, <var class="var">varargin</var>)</code><a class="copiable-link" href="#index-cellindexmat"> ¶</a></span></dt>
<dd><p>Perform indexing of matrices in a cell array.
</p>
<p>Given a cell array of matrices <var class="var">x</var>, this function computes
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">Y = cell (size (X));
for i = 1:numel (X)
Y{i} = X{i}(varargin{1}, varargin{2}, ..., varargin{N});
endfor
</pre></div></div>
<p>The indexing arguments may be scalar (<code class="code">2</code>), arrays (<code class="code">[1, 3]</code>),
ranges (<code class="code">1:3</code>), or the colon operator (<code class="code">":"</code>). However, the
indexing keyword <code class="code">end</code> is not available.
</p>
<p><strong class="strong">See also:</strong> <a class="ref" href="Creating-Cell-Arrays.html#XREFcellslices">cellslices</a>, <a class="ref" href="Function-Application.html#XREFcellfun">cellfun</a>.
</p></dd></dl>
</div>
<hr>
<div class="nav-panel">
<p>
Next: <a href="Cell-Arrays-of-Strings.html">Cell Arrays of Strings</a>, Previous: <a href="Creating-Cell-Arrays.html">Creating Cell Arrays</a>, Up: <a href="Cell-Arrays.html">Cell Arrays</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>
|