Skip to Main Content Skip to Search
Product Documentation

num2cell - Convert array to cell array with consistently sized cells

Syntax

C = num2cell(A)
C = num2cell(A,dim)
C = num2cell(A,[dim1,...,dimN])

Description

C = num2cell(A) converts array A into cell array C by placing each element of A into a separate cell in C.

C = num2cell(A,dim) creates vectors in cells of C that contain elements from the specified dimension dim of A.

C = num2cell(A,[dim1,...,dimN]) creates arrays in cells of C that contain elements from the specified dimensions of A.

Input Arguments

A

Any type of array.

dim

Scalar integer between 1 and ndims(A), where dim == 1 corresponds to the rows of A, dim == 2 corresponds to the columns, and so on.

The num2cell function combines elements from A along dimension dim to create cells of array C.

For example, given a 4-by-7-by-3 array A, this figure shows how num2cell creates cells corresponding to dim values of 1, 2, or 3.

dim1,...,dimN

Scalar integers between 1 and ndims(A) that specify the dimensions to combine within cells of C.

Output Arguments

C

Cell array that contains data from input array A. The dimensions of the cell array and its cells depend upon the inputs to the num2cell function:

  • If you do not specify any dimensions, then C has the same dimensions as the input array, unless A is empty. If A is empty, all dimensions of C are zero.

  • If you specify a single dimension dim, then C contains numel(A)/size(A,dim) vectors, each of length size(A, dim).

  • If you specify a vector of dimensions [dim1,...,dimN], then C contains numel(A)/prod(size(A,dim1),...,size(A,dimN)) arrays, each with dimensions size(A,dim1)-by-...-size(A,dimN).

Examples

To run the examples in this section, first create a three-dimensional numeric array.

a = reshape(1:12,4,3)';
a(:,:,2) = a * 10

a is 3-by-4-by-2 array:

a(:,:,1) =
     1     2     3     4
     5     6     7     8
     9    10    11    12
a(:,:,2) =
    10    20    30    40
    50    60    70    80
    90   100   110   120
 

Convert numeric array a (created in the previous example) to a cell array with the same dimensions.

c = num2cell(a);

c is a 3-by-4-by-2 cell array, where each cell contains a scalar numeric value:

c(:,:,1) = 
    [1]    [ 2]    [ 3]    [ 4]
    [5]    [ 6]    [ 7]    [ 8]
    [9]    [10]    [11]    [12]
c(:,:,2) = 
    [10]    [ 20]    [ 30]    [ 40]
    [50]    [ 60]    [ 70]    [ 80]
    [90]    [100]    [110]    [120]
 

Combine the elements from the first dimension of array a (created in a previous example) into vectors within cells of cell array c2.

c2 = num2cell(a, 1)

Each cell of c2 contains a 3-by-1 vector:

c2(:,:,1) = 
    [3x1 double]    [3x1 double]    [3x1 double]    [3x1 double]

c2(:,:,2) = 
    [3x1 double]    [3x1 double]    [3x1 double]    [3x1 double]

where, for example, c2{1} returns

ans =
     1
     5
     9
 

Combine the elements in the first and second dimensions of array a (created in a previous example) into arrays within cells of cell array c3.

c3 = num2cell(a, [1, 2])

Each cell of c3 contains a 3-by-4 numeric array:

c3(:,:,1) = 
    [3x4 double]

c3(:,:,2) = 
    [3x4 double]

where, for example, c3{1} returns

ans =
     1     2     3     4
     5     6     7     8
     9    10    11    12

See Also

cat | cell2mat | mat2cell

  


» Learn more
» Download free kit
» Get trial software

 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS