Products & Services Solutions Academia Support User Community Company

Learn more about MATLAB   

num2cell - Convert numeric array to cell array

Syntax

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

Description

C = num2cell(A) converts numeric array A into cell array C by placing each element of A into a separate cell in C. The output array has the same size and dimensions as the input array. Each cell in C contains the same numeric value as its respective element in A.

C = num2cell(A, dim) converts numeric array A into a cell array of numeric vectors, the dimensions of which depend on the value of the dim argument. Return value C contains numel(A)/size(A,dim) vectors, each of length size(A, dim). (See Example 2, below). The dim input must be an integer with a value from ndims(A) to 1.

C = num2cell(A, [dim1, dim2, ...]) converts numeric array A into a cell array of numeric arrays, the dimensions of which depend on the values of arguments [dim1, dim2, ...]. Given the variables X and Y, where X=size(A,dim1) and Y=size(A,dim2), return value C contains numel(A)/prod(X,Y,...) arrays, each of size X-by-Y-by-.... All dimN inputs must be an integer with a value from ndims(A) to 1.

Remarks

num2cell works for all array types.

To convert the cell array back to a numeric array, use cell2mat or cat(dim, c{:}). See Example 2 — Converting to Cell Array and Back to Numeric.

Examples

Example 1 — Converting to a Cell Array of the Same Dimensions

Create a 4x7x3 array of random numbers:

A = rand(4,7,3);

Convert the numeric array to a cell array of the same dimensions:

C = num2cell(A);
whos C
  Name      Size             Bytes  Class    Attributes

  C         4x7x3             5712  cell  

Example 2 — Converting to Cell Array and Back to Numeric

Create a 3-by-4 numeric array A:

A = [5:3:14; -9:4:3; 20:-4:8]
A =
     5     8    11    14
    -9    -5    -1     3
    20    16    12     8

Convert A to a cell array C1 of the same size. Convert it back to a numeric array using the cell2mat function:

C1 = num2cell(A)                  N1 = cell2mat(C1)
C1 =                              N1 =
   [5]   [ 8]   [11]   [14]           5    8   11   14
  [-9]   [-5]   [-1]   [ 3]          -9   -5   -1    3
  [20]   [16]   [12]   [ 8]          20   16   12    8

Convert A to a cell array C2 of 1-by-4 arrays. Convert it back to a numeric array using the cat function:

C2 = num2cell(A, 2)               N2 = cat(1, C2{:})
C2 =                              N2 =
    [1x4 double]                      5    8   11   14
    [1x4 double]                     -9   -5   -1    3
    [1x4 double]                     20   16   12    8

Example 3 — Converting to a Cell Array of Different Dimensions

The following diagrams show the output of C=num2cell(A,dim), where A is a 4x7x3 numeric array, and dim is one dimension of the input array: either 1, 2, or 3 in this case. The shaded segment of each diagram represents one cell of the output cell array. Each of these cells contains those elements of A that are positioned along the dimension specified by dim:

 

Example 4 — Specifying More Than One Dimension for the Output

Given a 4x7x3 numeric array A

A = rand(4,7,3)

and the following two values X and Y

X = size(A,dim1)
Y = size(A,dim2)

then the statement

C = num2cell(A, [1 3])

returns in C a cell array of numel(A)/prod(X,Y) numeric arrays, each having the dimensions X-by-Y.

See Also

cat, mat2cell, cell2mat

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

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