Sophie

Sophie

distrib > Mageia > 4 > x86_64 > by-pkgid > b38d2da330d1936e5ab1307c039c4941 > files > 306

octave-doc-3.6.4-3.mga4.noarch.rpm

<html lang="en">
<head>
<title>Indexing Cell Arrays - 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="Cell-Arrays.html#Cell-Arrays" title="Cell Arrays">
<link rel="prev" href="Creating-Cell-Arrays.html#Creating-Cell-Arrays" title="Creating Cell Arrays">
<link rel="next" href="Cell-Arrays-of-Strings.html#Cell-Arrays-of-Strings" title="Cell Arrays of Strings">
<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="Indexing-Cell-Arrays"></a>
<p>
Next:&nbsp;<a rel="next" accesskey="n" href="Cell-Arrays-of-Strings.html#Cell-Arrays-of-Strings">Cell Arrays of Strings</a>,
Previous:&nbsp;<a rel="previous" accesskey="p" href="Creating-Cell-Arrays.html#Creating-Cell-Arrays">Creating Cell Arrays</a>,
Up:&nbsp;<a rel="up" accesskey="u" href="Cell-Arrays.html#Cell-Arrays">Cell Arrays</a>
<hr>
</div>

<h4 class="subsection">6.2.3 Indexing Cell Arrays</h4>

<p>As shown in see <a href="Basic-Usage-of-Cell-Arrays.html#Basic-Usage-of-Cell-Arrays">Basic Usage of Cell Arrays</a> elements can be
extracted from cell arrays using the &lsquo;<samp><span class="samp">{</span></samp>&rsquo; and &lsquo;<samp><span class="samp">}</span></samp>&rsquo;
operators.  If you want to extract or access subarrays which are still
cell arrays, you need to use the &lsquo;<samp><span class="samp">(</span></samp>&rsquo; and &lsquo;<samp><span class="samp">)</span></samp>&rsquo; operators.  The
following example illustrates the difference:

<pre class="example">     c = {"1", "2", "3"; "a", "b", "c"; "4", "5", "6"};
     c{2,3}
          &rArr; ans = c
     
     c(2,3)
          &rArr; ans =
             {
               [1,1] = c
             }
</pre>
   <p class="noindent">So with &lsquo;<samp><span class="samp">{}</span></samp>&rsquo; you access elements of a cell
array, while with &lsquo;<samp><span class="samp">()</span></samp>&rsquo; you access a sub array of a cell
array.

   <p>Using the &lsquo;<samp><span class="samp">(</span></samp>&rsquo; and &lsquo;<samp><span class="samp">)</span></samp>&rsquo; 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>0</code>
with the following command:

<pre class="example">     c(:, [1, 3]) = {0}
          &rArr;  =
             {
               [1,1] = 0
               [2,1] = 0
               [3,1] = 0
               [1,2] = 2
               [2,2] =  10
               [3,2] =  20
               [1,3] = 0
               [2,3] = 0
               [3,3] = 0
             }
</pre>
   <p>Note, that the above can also be achieved like this:

<pre class="example">     c(:, [1, 3]) = 0;
</pre>
   <p class="noindent">Here, the scalar &lsquo;<samp><span class="samp">0</span></samp>&rsquo; is automatically promoted to
cell array &lsquo;<samp><span class="samp">{0}</span></samp>&rsquo; and then assigned to the subarray of <code>c</code>.

   <p>To give another example for indexing cell arrays with &lsquo;<samp><span class="samp">()</span></samp>&rsquo;, you
can exchange the first and the second row of a cell array as in the
following command:

<pre class="example">     c = {1, 2, 3; 4, 5, 6};
     c([1, 2], :) = c([2, 1], :)
          &rArr; =
             {
               [1,1] =  4
               [2,1] =  1
               [1,2] =  5
               [2,2] =  2
               [1,3] =  6
               [2,3] =  3
             }
</pre>
   <p>Accessing multiple elements of a cell array with the &lsquo;<samp><span class="samp">{</span></samp>&rsquo; and
&lsquo;<samp><span class="samp">}</span></samp>&rsquo; operators will result in a comma-separated list of all the
requested elements (see <a href="Comma-Separated-Lists.html#Comma-Separated-Lists">Comma Separated Lists</a>).  Using the
&lsquo;<samp><span class="samp">{</span></samp>&rsquo; and &lsquo;<samp><span class="samp">}</span></samp>&rsquo; operators the first two rows in the above
example can be swapped back like this:

<pre class="example">     [c{[1,2], :}] = deal(c{[2, 1], :})
          &rArr; =
             {
               [1,1] =  1
               [2,1] =  4
               [1,2] =  2
               [2,2] =  5
               [1,3] =  3
               [2,3] =  6
             }
</pre>
   <p>As for struct arrays and numerical arrays, the empty matrix &lsquo;<samp><span class="samp">[]</span></samp>&rsquo;
can be used to delete elements from a cell array:

<pre class="example">     x = {"1", "2"; "3", "4"};
     x(1, :) = []
          &rArr; x =
             {
               [1,1] = 3
               [1,2] = 4
             }
</pre>
   <p>The following example shows how to just remove the contents of cell
array elements but not delete the space for them:

<pre class="example">     x = {"1", "2"; "3", "4"};
     x{1, :} = []
     &rArr; x =
           {
             [1,1] = [](0x0)
             [2,1] = 3
             [1,2] = [](0x0)
             [2,2] = 4
           }
</pre>
   <p>The indexing operations operate on the cell array and not on the objects
within the cell array.  By contrast, <code>cellindexmat</code> applies matrix indexing
to the objects within each cell array entry and returns the requested values.

<!-- cellindexmat src/DLD-FUNCTIONS/cellfun.cc -->
   <p><a name="doc_002dcellindexmat"></a>

<div class="defun">
&mdash; Loadable Function: <var>y</var> = <b>cellindexmat</b> (<var>x, varargin</var>)<var><a name="index-cellindexmat-469"></a></var><br>
<blockquote><p>Given a cell array of matrices <var>x</var>, this function computes

     <pre class="example">            Y = cell (size (X));
            for i = 1:numel (X)
              Y{i} = X{i}(varargin{:});
            endfor
</pre>
        <!-- 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_002dcellslices.html#doc_002dcellslices">cellslices</a>, <a href="doc_002dcellfun.html#doc_002dcellfun">cellfun</a>. 
</p></blockquote></div>

   </body></html>