- publishing free software manuals
GNU Octave Manual Version 3
by John W. Eaton, David Bateman, Søren Hauberg
Paperback (6"x9"), 568 pages
ISBN 095461206X
RRP £24.95 ($39.95)

Get a printed copy>>>

6.2.1 Creating Cell Array

The introductory example 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.

The cell function returns a cell array of a given size, containing empty matrices. This function is similar to to the zeros function for creating new numerical arrays. The following example creates a 2-by-2 cell array containing empty matrices

c = cell(2,2)
     => c =
           [1,1] = [](0x0)
           [2,1] = [](0x0)
           [1,2] = [](0x0)
           [2,2] = [](0x0)

Just like numerical arrays, cell arrays can be multidimensional. The cell 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

c1 = cell(3, 4, 5);
c2 = cell( [3, 4, 5] );
     => ans =
         3   4   5

As can be seen, the size function also works for cell arrays. As do the other functions describing the size of an object, such as length, numel, rows, and columns.

As an alternative to creating empty cell arrays, and then filling them, it is possible to convert numerical arrays into cell arrays using the num2cell and mat2cell functions.

Built-in Function: cell (x)
Built-in Function: cell (n, m)
Create a new cell array object. If invoked with a single scalar argument, cell returns a square cell array with the dimension specified. If you supply two scalar arguments, cell takes them to be the number of rows and columns. If given a vector with two elements, cell uses the values of the elements as the number of rows and columns, respectively.

Built-in Function: iscell (x)
Return true if x is a cell array object. Otherwise, return false.

Loadable Function: c = num2cell (m)
Loadable Function: c = num2cell (m, d)
Convert to matrix m into a cell array. If d is defined the value c is of dimension 1 in this dimension and the elements of m are placed in slices in c.

See also mat2cell

Loadable Function: b = mat2cell (a, m, n)
Loadable Function: b = mat2cell (a, d1, d2, ...)
Loadable Function: b = mat2cell (a, r)
Converts the matrix a to a cell array If a is 2-D, then it is required that sum (m) == size (a, 1) and sum (n) == size (a, 2). Similarly, if a is a multi-dimensional and the number of dimensional arguments is equal to the dimensions of a, then it is required that sum (di) == size (a, i).

Given a single dimensional argument r, the other dimensional arguments are assumed to equal size (a,i).

An example of the use of mat2cell is

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] =


  [2,2] = 16

See also num2cell, cell2mat

ISBN 095461206XGNU Octave Manual Version 3See the print edition