Next: , Previous: , Up: Cell Arrays   [Contents][Index]


6.3.2 Creating Cell Arrays

The introductory example (see 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 size function also works for cell arrays. As do other functions describing the size of an object, such as length, numel, rows, and 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: cellstr, mat2cell, num2cell, 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: 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: num2cell, cell2mat.

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: cell2mat, cellindexmat, cellfun.


Next: , Previous: , Up: Cell Arrays   [Contents][Index]