(octave.info)Creating Cell Arrays


Next: Indexing Cell Arrays Prev: Basic Usage of Cell Arrays Up: Cell Arrays
Enter node , (file) or (file)node

6.3.2 Creating Cell Arrays
--------------------------

The introductory example (Note: Basic Usage of Cell Arrays) 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 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 multi-dimensional.
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] );
     size (c1)
          ⇒ ans =
              3   4   5

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

 -- : cell (N)
 -- : cell (M, N)
 -- : cell (M, N, K, ...)
 -- : cell ([M N ...])
     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.

     See also: Note: cellstr, Note: mat2cell,
     Note: num2cell, Note: struct2cell.

   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’, ‘mat2cell’ and ‘cellslices’ functions.

 -- : C = num2cell (A)
 -- : C = num2cell (A, DIM)
     Convert the numeric matrix A to a cell array.

     If DIM is defined, the value C is of dimension 1 in this dimension
     and the elements of A are placed into C in slices.  For example:

          num2cell ([1,2;3,4])
             ⇒
                {
                  [1,1] =  1
                  [2,1] =  3
                  [1,2] =  2
                  [2,2] =  4
                }
          num2cell ([1,2;3,4],1)
             ⇒
                {
                  [1,1] =
                     1
                     3
                  [1,2] =
                     2
                     4
                }

     See also: Note: mat2cell.

 -- : C = mat2cell (A, M, N)
 -- : C = mat2cell (A, D1, D2, ...)
 -- : C = mat2cell (A, R)
     Convert 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 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] =

               13
               14
               15

             [2,2] = 16
          }

     See also: Note: num2cell, *note cell2mat:
     XREFcell2mat.

 -- : SL = cellslices (X, LB, UB, DIM)
     Given an array X, this function produces a cell array of slices
     from the array determined by the index vectors LB, UB, for lower
     and upper bounds, respectively.

     In other words, it is equivalent to the following code:

          n = length (lb);
          sl = cell (1, n);
          for i = 1:length (lb)
            sl{i} = x(:,...,lb(i):ub(i),...,:);
          endfor

     The position of the index is determined by DIM.  If not specified,
     slicing is done along the first non-singleton dimension.

     See also: Note: cell2mat, *note cellindexmat:
     XREFcellindexmat, Note: cellfun.


automatically generated by info2www version 1.2.2.9