<html lang="en"> <head> <title>Creating 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="Basic-Usage-of-Cell-Arrays.html#Basic-Usage-of-Cell-Arrays" title="Basic Usage of Cell Arrays"> <link rel="next" href="Indexing-Cell-Arrays.html#Indexing-Cell-Arrays" title="Indexing Cell Arrays"> <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="Creating-Cell-Arrays"></a> <p> Next: <a rel="next" accesskey="n" href="Indexing-Cell-Arrays.html#Indexing-Cell-Arrays">Indexing Cell Arrays</a>, Previous: <a rel="previous" accesskey="p" href="Basic-Usage-of-Cell-Arrays.html#Basic-Usage-of-Cell-Arrays">Basic Usage of Cell Arrays</a>, Up: <a rel="up" accesskey="u" href="Cell-Arrays.html#Cell-Arrays">Cell Arrays</a> <hr> </div> <h4 class="subsection">6.2.2 Creating Cell Array</h4> <p>The introductory example (see <a href="Basic-Usage-of-Cell-Arrays.html#Basic-Usage-of-Cell-Arrays">Basic Usage of Cell Arrays</a>) showed how to create a cell array containing currently available variables. In many situations, however, it is useful to create a cell array and then fill it with data. <p>The <code>cell</code> function returns a cell array of a given size, containing empty matrices. This function is similar to the <code>zeros</code> function for creating new numerical arrays. The following example creates a 2-by-2 cell array containing empty matrices <pre class="example"> c = cell(2,2) ⇒ c = { [1,1] = [](0x0) [2,1] = [](0x0) [1,2] = [](0x0) [2,2] = [](0x0) } </pre> <p>Just like numerical arrays, cell arrays can be multi-dimensional. The <code>cell</code> function accepts any number of positive integers to describe the size of the returned cell array. It is also possible to set the size of the cell array through a vector of positive integers. In the following example two cell arrays of equal size are created, and the size of the first one is displayed <pre class="example"> c1 = cell(3, 4, 5); c2 = cell( [3, 4, 5] ); size(c1) ⇒ ans = 3 4 5 </pre> <p class="noindent">As can be seen, the <a href="doc_002dsize.html#doc_002dsize"><code>size</code></a> function also works for cell arrays. As do other functions describing the size of an object, such as <a href="doc_002dlength.html#doc_002dlength"><code>length</code></a>, <a href="doc_002dnumel.html#doc_002dnumel"><code>numel</code></a>, <a href="doc_002drows.html#doc_002drows"><code>rows</code></a>, and <a href="doc_002dcolumns.html#doc_002dcolumns"><code>columns</code></a>. <!-- cell src/ov-cell.cc --> <p><a name="doc_002dcell"></a> <div class="defun"> — Built-in Function: <b>cell</b> (<var>n</var>)<var><a name="index-cell-459"></a></var><br> — Built-in Function: <b>cell</b> (<var>m, n</var>)<var><a name="index-cell-460"></a></var><br> — Built-in Function: <b>cell</b> (<var>m, n, k, <small class="dots">...</small></var>)<var><a name="index-cell-461"></a></var><br> — Built-in Function: <b>cell</b> ([<var>m n <small class="dots">...</small></var>])<var><a name="index-cell-462"></a></var><br> <blockquote><p>Create a new cell array object. If invoked with a single scalar integer argument, return a square NxN cell array. If invoked with two or more scalar integer arguments, or a vector of integer values, return an array with the given dimensions. </p></blockquote></div> <p>As an alternative to creating empty cell arrays, and then filling them, it is possible to convert numerical arrays into cell arrays using the <code>num2cell</code>, <code>mat2cell</code> and <code>cellslices</code> functions. <!-- num2cell src/DLD-FUNCTIONS/cellfun.cc --> <p><a name="doc_002dnum2cell"></a> <div class="defun"> — Loadable Function: <var>C</var> = <b>num2cell</b> (<var>A</var>)<var><a name="index-num2cell-463"></a></var><br> — Loadable Function: <var>C</var> = <b>num2cell</b> (<var>A, dim</var>)<var><a name="index-num2cell-464"></a></var><br> <blockquote><p>Convert the numeric matrix <var>A</var> to a cell array. If <var>dim</var> is defined, the value <var>C</var> is of dimension 1 in this dimension and the elements of <var>A</var> are placed into <var>C</var> in slices. For example: <pre class="example"> num2cell([1,2;3,4]) ⇒ ans = { [1,1] = 1 [2,1] = 3 [1,2] = 2 [2,2] = 4 } num2cell([1,2;3,4],1) ⇒ ans = { [1,1] = 1 3 [1,2] = 2 4 } </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_002dmat2cell.html#doc_002dmat2cell">mat2cell</a>. </p></blockquote></div> <!-- mat2cell src/DLD-FUNCTIONS/cellfun.cc --> <p><a name="doc_002dmat2cell"></a> <div class="defun"> — Loadable Function: <var>C</var> = <b>mat2cell</b> (<var>A, m, n</var>)<var><a name="index-mat2cell-465"></a></var><br> — Loadable Function: <var>C</var> = <b>mat2cell</b> (<var>A, d1, d2, <small class="dots">...</small></var>)<var><a name="index-mat2cell-466"></a></var><br> — Loadable Function: <var>C</var> = <b>mat2cell</b> (<var>A, r</var>)<var><a name="index-mat2cell-467"></a></var><br> <blockquote><p>Convert the matrix <var>A</var> to a cell array. If <var>A</var> is 2-D, then it is required that <code>sum (</code><var>m</var><code>) == size (</code><var>A</var><code>, 1)</code> and <code>sum (</code><var>n</var><code>) == size (</code><var>A</var><code>, 2)</code>. Similarly, if <var>A</var> is multi-dimensional and the number of dimensional arguments is equal to the dimensions of <var>A</var>, then it is required that <code>sum (</code><var>di</var><code>) == size (</code><var>A</var><code>, i)</code>. <p>Given a single dimensional argument <var>r</var>, the other dimensional arguments are assumed to equal <code>size (</code><var>A</var><code>,</code><var>i</var><code>)</code>. <p>An example of the use of mat2cell is <pre class="example"> mat2cell (reshape(1:16,4,4),[3,1],[3,1]) ⇒ { [1,1] = 1 5 9 2 6 10 3 7 11 [2,1] = 4 8 12 [1,2] = 13 14 15 [2,2] = 16 } </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_002dnum2cell.html#doc_002dnum2cell">num2cell</a>, <a href="doc_002dcell2mat.html#doc_002dcell2mat">cell2mat</a>. </p></blockquote></div> <!-- cellslices src/DLD-FUNCTIONS/cellfun.cc --> <p><a name="doc_002dcellslices"></a> <div class="defun"> — Loadable Function: <var>sl</var> = <b>cellslices</b> (<var>x, lb, ub, dim</var>)<var><a name="index-cellslices-468"></a></var><br> <blockquote><p>Given an array <var>x</var>, this function produces a cell array of slices from the array determined by the index vectors <var>lb</var>, <var>ub</var>, for lower and upper bounds, respectively. In other words, it is equivalent to the following code: <pre class="example"> n = length (lb); sl = cell (1, n); for i = 1:length (lb) sl{i} = x(:,...,lb(i):ub(i),...,:); endfor </pre> <p>The position of the index is determined by <var>dim</var>. If not specified, slicing is done along the first non-singleton dimension. <!-- 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_002dcell2mat.html#doc_002dcell2mat">cell2mat</a>, <a href="doc_002dcellindexmat.html#doc_002dcellindexmat">cellindexmat</a>, <a href="doc_002dcellfun.html#doc_002dcellfun">cellfun</a>. </p></blockquote></div> </body></html>