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
|
<html lang="en">
<head>
<title>Creating Cell Arrays - Untitled</title>
<meta http-equiv="Content-Type" content="text/html">
<meta name="description" content="Untitled">
<meta name="generator" content="makeinfo 4.11">
<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">
<p>
<a name="Creating-Cell-Arrays"></a>
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 multidimensional. 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>.
<!-- ov-cell.cc -->
<p><a name="doc_002dcell"></a>
<div class="defun">
— Built-in Function: <b>cell</b> (<var>x</var>)<var><a name="index-cell-390"></a></var><br>
— Built-in Function: <b>cell</b> (<var>n, m</var>)<var><a name="index-cell-391"></a></var><br>
<blockquote><p>Create a new cell array object. If invoked with a single scalar
argument, <code>cell</code> returns a square cell array with the dimension
specified. If you supply two scalar arguments, <code>cell</code> takes
them to be the number of rows and columns. If given a vector with two
elements, <code>cell</code> uses the values of the elements as the number of
rows and columns, respectively.
</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> and <code>mat2cell</code> functions.
<!-- ./DLD-FUNCTIONS/cellfun.cc -->
<p><a name="doc_002dnum2cell"></a>
<div class="defun">
— Loadable Function: <var>c</var> = <b>num2cell</b> (<var>m</var>)<var><a name="index-num2cell-392"></a></var><br>
— Loadable Function: <var>c</var> = <b>num2cell</b> (<var>m, dim</var>)<var><a name="index-num2cell-393"></a></var><br>
<blockquote><p>Convert the matrix <var>m</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>m</var> are placed in slices in <var>c</var>.
<!-- 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>
<!-- ./DLD-FUNCTIONS/cellfun.cc -->
<p><a name="doc_002dmat2cell"></a>
<div class="defun">
— Loadable Function: <var>b</var> = <b>mat2cell</b> (<var>a, m, n</var>)<var><a name="index-mat2cell-394"></a></var><br>
— Loadable Function: <var>b</var> = <b>mat2cell</b> (<var>a, d1, d2, <small class="dots">...</small></var>)<var><a name="index-mat2cell-395"></a></var><br>
— Loadable Function: <var>b</var> = <b>mat2cell</b> (<var>a, r</var>)<var><a name="index-mat2cell-396"></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
a 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>
</body></html>
|