<!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>Creating Cell Arrays (GNU Octave (version 5.1.0))</title> <meta name="description" content="Creating Cell Arrays (GNU Octave (version 5.1.0))"> <meta name="keywords" content="Creating Cell Arrays (GNU Octave (version 5.1.0))"> <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="Cell-Arrays.html#Cell-Arrays" rel="up" title="Cell Arrays"> <link href="Indexing-Cell-Arrays.html#Indexing-Cell-Arrays" rel="next" title="Indexing Cell Arrays"> <link href="Basic-Usage-of-Cell-Arrays.html#Basic-Usage-of-Cell-Arrays" rel="prev" title="Basic Usage of Cell Arrays"> <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="Creating-Cell-Arrays"></a> <div class="header"> <p> Next: <a href="Indexing-Cell-Arrays.html#Indexing-Cell-Arrays" accesskey="n" rel="next">Indexing Cell Arrays</a>, Previous: <a href="Basic-Usage-of-Cell-Arrays.html#Basic-Usage-of-Cell-Arrays" accesskey="p" rel="prev">Basic Usage of Cell Arrays</a>, Up: <a href="Cell-Arrays.html#Cell-Arrays" 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#Concept-Index" title="Index" rel="index">Index</a>]</p> </div> <hr> <a name="Creating-Cell-Arrays-1"></a> <h4 class="subsection">6.3.2 Creating Cell Arrays</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> <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 </p> <div class="example"> <pre class="example">c = cell (2,2) ⇒ c = { [1,1] = [](0x0) [2,1] = [](0x0) [1,2] = [](0x0) [2,2] = [](0x0) } </pre></div> <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 </p> <div class="example"> <pre class="example">c1 = cell (3, 4, 5); c2 = cell ( [3, 4, 5] ); size (c1) ⇒ ans = 3 4 5 </pre></div> <p>As can be seen, the <a href="Object-Sizes.html#XREFsize">size</a> function also works for cell arrays. As do other functions describing the size of an object, such as <a href="Object-Sizes.html#XREFlength">length</a>, <a href="Object-Sizes.html#XREFnumel">numel</a>, <a href="Object-Sizes.html#XREFrows">rows</a>, and <a href="Object-Sizes.html#XREFcolumns">columns</a>. </p> <a name="XREFcell"></a><dl> <dt><a name="index-cell"></a><em></em> <strong>cell</strong> <em>(<var>n</var>)</em></dt> <dt><a name="index-cell-1"></a><em></em> <strong>cell</strong> <em>(<var>m</var>, <var>n</var>)</em></dt> <dt><a name="index-cell-2"></a><em></em> <strong>cell</strong> <em>(<var>m</var>, <var>n</var>, <var>k</var>, …)</em></dt> <dt><a name="index-cell-3"></a><em></em> <strong>cell</strong> <em>([<var>m</var> <var>n</var> …])</em></dt> <dd><p>Create a new cell array object. </p> <p>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> <p><strong>See also:</strong> <a href="Cell-Arrays-of-Strings.html#XREFcellstr">cellstr</a>, <a href="#XREFmat2cell">mat2cell</a>, <a href="#XREFnum2cell">num2cell</a>, <a href="Processing-Data-in-Structures.html#XREFstruct2cell">struct2cell</a>. </p></dd></dl> <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. </p> <a name="XREFnum2cell"></a><dl> <dt><a name="index-num2cell"></a><em><var>C</var> =</em> <strong>num2cell</strong> <em>(<var>A</var>)</em></dt> <dt><a name="index-num2cell-1"></a><em><var>C</var> =</em> <strong>num2cell</strong> <em>(<var>A</var>, <var>dim</var>)</em></dt> <dd><p>Convert the numeric matrix <var>A</var> to a cell array. </p> <p>When no <var>dim</var> is specified, each element of <var>A</var> becomes a 1x1 element in the output <var>C</var>. </p> <p>If <var>dim</var> is defined then individual elements of <var>C</var> contain all of the elements from <var>A</var> along the specified dimension. <var>dim</var> may also be a vector of dimensions with the same rule applied. </p> <p>For example: </p> <div class="example"> <pre class="example">x = [1,2;3,4] ⇒ 1 2 3 4 ## each element of A becomes a 1x1 element of C num2cell (x) ⇒ { [1,1] = 1 [2,1] = 3 [1,2] = 2 [2,2] = 4 } ## all rows (dim 1) of A appear in each element of C num2cell (x, 1) ⇒ { [1,1] = 1 3 [1,2] = 2 4 } ## all columns (dim 2) of A appear in each element of C num2cell (x, 2) ⇒ { [1,1] = 1 2 [2,1] = 3 4 } ## all rows and cols appear in each element of C ## (hence, only 1 output) num2cell (x, [1, 2]) ⇒ { [1,1] = 1 2 3 4 } </pre></div> <p><strong>See also:</strong> <a href="#XREFmat2cell">mat2cell</a>. </p></dd></dl> <a name="XREFmat2cell"></a><dl> <dt><a name="index-mat2cell"></a><em><var>C</var> =</em> <strong>mat2cell</strong> <em>(<var>A</var>, <var>dim1</var>, <var>dim2</var>, …, <var>dimi</var>, …, <var>dimn</var>)</em></dt> <dt><a name="index-mat2cell-1"></a><em><var>C</var> =</em> <strong>mat2cell</strong> <em>(<var>A</var>, <var>rowdim</var>)</em></dt> <dd><p>Convert the matrix <var>A</var> to a cell array. </p> <p>Each dimension argument (<var>dim1</var>, <var>dim2</var>, etc.) is a vector of integers which specifies how to divide that dimension’s elements amongst the new elements in the output <var>C</var>. The number of elements in the <var>i</var>-th dimension is <code>size (<var>A</var>, <var>i</var>)</code>. Because all elements in <var>A</var> must be partitioned, there is a requirement that <code>sum (<var>di</var>) == size (<var>A</var>, i)</code>. The size of the output cell <var>C</var> is numel (<var>dim1</var>) x numel (<var>dim2</var>) x … x numel (<var>dimn</var>). </p> <p>Given a single dimensional argument, <var>rowdim</var>, the output is divided into rows as specified. All other dimensions are not divided and thus all columns (dim 2), pages (dim 3), etc. appear in each output element. </p> <p>Examples </p> <div class="example"> <pre class="example">x = reshape (1:12, [3, 4])' ⇒ 1 2 3 4 5 6 7 8 9 10 11 12 </pre><pre class="example">## The 4 rows (dim1) are divided in to two cell elements ## with 2 rows each. ## The 3 cols (dim2) are divided in to three cell elements ## with 1 col each. mat2cell (x, [2,2], [1,1,1]) ⇒ { [1,1] = 1 4 [2,1] = 7 10 [1,2] = 2 5 [2,2] = 8 11 [1,3] = 3 6 [2,3] = 9 12 } </pre><pre class="example"> </pre><pre class="example">## The 4 rows (dim1) are divided in to two cell elements ## with a 3/1 split. ## All columns appear in each output element. mat2cell (x, [3,1]) ⇒ { [1,1] = 1 2 3 4 5 6 7 8 9 [2,1] = 10 11 12 } </pre></div> <p><strong>See also:</strong> <a href="#XREFnum2cell">num2cell</a>, <a href="Processing-Data-in-Cell-Arrays.html#XREFcell2mat">cell2mat</a>. </p></dd></dl> <a name="XREFcellslices"></a><dl> <dt><a name="index-cellslices"></a><em><var>sl</var> =</em> <strong>cellslices</strong> <em>(<var>x</var>, <var>lb</var>, <var>ub</var>, <var>dim</var>)</em></dt> <dd><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. </p> <p>In other words, it is equivalent to the following code: </p> <div class="example"> <pre class="example">n = length (lb); sl = cell (1, n); for i = 1:length (lb) sl{i} = x(:,…,lb(i):ub(i),…,:); endfor </pre></div> <p>The position of the index is determined by <var>dim</var>. If not specified, slicing is done along the first non-singleton dimension. </p> <p><strong>See also:</strong> <a href="Processing-Data-in-Cell-Arrays.html#XREFcell2mat">cell2mat</a>, <a href="Indexing-Cell-Arrays.html#XREFcellindexmat">cellindexmat</a>, <a href="Function-Application.html#XREFcellfun">cellfun</a>. </p></dd></dl> <hr> <div class="header"> <p> Next: <a href="Indexing-Cell-Arrays.html#Indexing-Cell-Arrays" accesskey="n" rel="next">Indexing Cell Arrays</a>, Previous: <a href="Basic-Usage-of-Cell-Arrays.html#Basic-Usage-of-Cell-Arrays" accesskey="p" rel="prev">Basic Usage of Cell Arrays</a>, Up: <a href="Cell-Arrays.html#Cell-Arrays" 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#Concept-Index" title="Index" rel="index">Index</a>]</p> </div> </body> </html>