num2cell - Convert array to cell array with consistently sized cells - MATLAB (original) (raw)

Convert array to cell array with consistently sized cells

Syntax

Description

[C](#btj8u9n-C) = num2cell([A](#btj8u9n-A)) converts arrayA into cell array C by placing each element of A into a separate cell inC.

The num2cell function converts an array that has any data type—even a nonnumeric type.

example

[C](#btj8u9n-C) = num2cell([A](#btj8u9n-A),[dim](#btj8u9n-dim)) splits the contents of A into separate cells of C, where dim specifies which dimensions of A to include in each cell. dim can be a scalar or a vector of dimensions. For example, if A has 2 rows and 3 columns, then:

example

Examples

collapse all

Place all elements of a numeric array into separate cells.

a = 3×3

 8     1     6
 3     5     7
 4     9     2

c=3×3 cell array {[8]} {[1]} {[6]} {[3]} {[5]} {[7]} {[4]} {[9]} {[2]}

Place individual letters of a word into separate cells of an array.

a = ['four';'five';'nine']

a = 3×4 char array 'four' 'five' 'nine'

c = 3×4 cell {'f'} {'o'} {'u'} {'r'} {'f'} {'i'} {'v'} {'e'} {'n'} {'i'} {'n'} {'e'}

Generate a 4-by-3-by-2 numeric array, and then create a 1-by-3-by-2 cell array of 4-by-1 column vectors.

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

A = A(:,:,1) =

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

A(:,:,2) =

10    50    90
20    60   100
30    70   110
40    80   120

C = 1×3×2 cell array C(:,:,1) =

{4×1 double}    {4×1 double}    {4×1 double}

C(:,:,2) =

{4×1 double}    {4×1 double}    {4×1 double}

Each 4-by-1 vector contains elements from along the first dimension of A:

Create a 4-by-1-by-2 cell array of 1-by-3 numeric arrays.

C = 4×1×2 cell array C(:,:,1) =

{[ 1 5 9]}
{[2 6 10]}
{[3 7 11]}
{[4 8 12]}

C(:,:,2) =

{[ 10 50 90]}
{[20 60 100]}
{[30 70 110]}
{[40 80 120]}

Each 1-by-3 row vector contains elements from along the second dimension of A:

Finally, create a 4-by-3 cell array of 1-by-1-by-2 numeric arrays.

C=4×3 cell array {1×1×2 double} {1×1×2 double} {1×1×2 double} {1×1×2 double} {1×1×2 double} {1×1×2 double} {1×1×2 double} {1×1×2 double} {1×1×2 double} {1×1×2 double} {1×1×2 double} {1×1×2 double}

Each 1-by-1-by-2 vector contains elements from along the third dimension of A:

ans = ans(:,:,1) =

 1

ans(:,:,2) =

10

Create a cell array by combining elements into numeric arrays along several dimensions.

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

A = A(:,:,1) =

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

A(:,:,2) =

10    50    90
20    60   100
30    70   110
40    80   120

c=1×3 cell array {4×1×2 double} {4×1×2 double} {4×1×2 double}

Each 4-by-1-by-2 array contains elements from along the first and third dimension of A:

ans = ans(:,:,1) =

 1
 2
 3
 4

ans(:,:,2) =

10
20
30
40

c=4×1 cell array {1×3×2 double} {1×3×2 double} {1×3×2 double} {1×3×2 double}

Input Arguments

collapse all

Input, specified as any type of multidimensional array.

Data Types: double | single | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical | char | string | struct | cell | categorical | datetime | duration | calendarDuration | function_handle

Dimension of A, specified as a positive integer or a vector of positive integers. dim must be between 1 and ndims(A).

Elements do not need to be in numeric order. However, num2cell permutes the dimensions of the arrays in each cell of C to match the order of the specified dimensions.

Data Types: double | single | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Output Arguments

collapse all

Resulting array, returned as a cell array. The size of C depends on the size of A and the values of dim.

Data Types: cell

Tips

Extended Capabilities

expand all

Usage notes and limitations:

Version History

Introduced before R2006a