File: Creating-Cell-Arrays.html

package info (click to toggle)
octave 6.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 124,192 kB
  • sloc: cpp: 322,665; ansic: 68,088; fortran: 20,980; objc: 8,121; sh: 7,719; yacc: 4,266; lex: 4,123; perl: 1,530; java: 1,366; awk: 1,257; makefile: 424; xml: 147
file content (296 lines) | stat: -rw-r--r-- 10,616 bytes parent folder | download
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- Created by GNU Texinfo 6.7, 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 6.2.0))</title>

<meta name="description" content="Creating Cell Arrays (GNU Octave (version 6.2.0))">
<meta name="keywords" content="Creating Cell Arrays (GNU Octave (version 6.2.0))">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<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="Indexing-Cell-Arrays.html" rel="next" title="Indexing Cell Arrays">
<link href="Basic-Usage-of-Cell-Arrays.html" rel="prev" title="Basic Usage of Cell Arrays">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.indentedblock {margin-right: 0em}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.lisp {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}
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">
<span id="Creating-Cell-Arrays"></span><div class="header">
<p>
Next: <a href="Indexing-Cell-Arrays.html" accesskey="n" rel="next">Indexing Cell Arrays</a>, Previous: <a href="Basic-Usage-of-Cell-Arrays.html" accesskey="p" rel="prev">Basic Usage of Cell Arrays</a>, Up: <a href="Cell-Arrays.html" accesskey="u" rel="up">Cell Arrays</a> &nbsp; [<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>
<span id="Creating-Cell-Arrays-1"></span><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</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)
     &rArr; 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)
     &rArr; 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>
<span id="XREFcell"></span><dl>
<dt id="index-cell">: <em></em> <strong>cell</strong> <em>(<var>n</var>)</em></dt>
<dt id="index-cell-1">: <em></em> <strong>cell</strong> <em>(<var>m</var>, <var>n</var>)</em></dt>
<dt id="index-cell-2">: <em></em> <strong>cell</strong> <em>(<var>m</var>, <var>n</var>, <var>k</var>, &hellip;)</em></dt>
<dt id="index-cell-3">: <em></em> <strong>cell</strong> <em>([<var>m</var> <var>n</var> &hellip;])</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>
<span id="XREFnum2cell"></span><dl>
<dt id="index-num2cell">: <em><var>C</var> =</em> <strong>num2cell</strong> <em>(<var>A</var>)</em></dt>
<dt id="index-num2cell-1">: <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]
&rArr;
    1    2
    3    4

## each element of A becomes a 1x1 element of C
num2cell (x)
   &rArr;
      {
        [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)
   &rArr;
      {
        [1,1] =
           1
           3
        [1,2] =
           2
           4
      }
## all columns (dim 2) of A appear in each element of C
num2cell (x, 2)
   &rArr;
      {
        [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])
   &rArr;
      {
        [1,1] =
           1   2
           3   4
      }
</pre></div>


<p><strong>See also:</strong> <a href="#XREFmat2cell">mat2cell</a>.
</p></dd></dl>


<span id="XREFmat2cell"></span><dl>
<dt id="index-mat2cell">: <em><var>C</var> =</em> <strong>mat2cell</strong> <em>(<var>A</var>, <var>dim1</var>, <var>dim2</var>, &hellip;, <var>dimi</var>, &hellip;, <var>dimn</var>)</em></dt>
<dt id="index-mat2cell-1">: <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&rsquo;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>dimi</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 &hellip; 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])'
&rArr;
    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])
&rArr;
{
  [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])
&rArr;
{
  [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>


<span id="XREFcellslices"></span><dl>
<dt id="index-cellslices">: <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(:,&hellip;,lb(i):ub(i),&hellip;,:);
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" accesskey="n" rel="next">Indexing Cell Arrays</a>, Previous: <a href="Basic-Usage-of-Cell-Arrays.html" accesskey="p" rel="prev">Basic Usage of Cell Arrays</a>, Up: <a href="Cell-Arrays.html" accesskey="u" rel="up">Cell Arrays</a> &nbsp; [<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>