num2cell
Convert array to cell array with consistently sized cells
Syntax
C = num2cell(A)
C = num2cell(A,dim)
Description
splits the contents ofC
= num2cell(A
,dim
)A
into separate cells ofC
, wheredim
specifies which dimensions ofA
to include in each cell.dim
can be a scalar or a vector of dimensions. For example, ifA
has 2 rows and 3 columns, then:
num2cell(A,1)
creates a 1-by-3 cell arrayC
, where each cell contains a 2-by-1 column ofA
.num2cell(A,2)
creates a 2-by-1 cell arrayC
, where each cell contains a 1-by-3 row ofA
.num2cell ([1 - 2])
creates a 1-by-1 cell arrayC
, where the cell contains the entire arrayA
.
Examples
Convert Arrays to Cell Array
Place all elements of a numeric array into separate cells.
a = magic(3)
a =8 1 6 3 5 7 4 9 2
c = num2cell(a)
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 = num2cell(a)
c =3×4 cell array'f' 'o' 'u' 'r' 'f' 'i' 'v' 'e' 'n' 'i' 'n' 'e'
Create Cell Array of Numeric Arrays
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 = (:,:,1) = 1 5 9 2 6 10 3 7 11 4 8 12 (:,:,2) = 10 50 90 20 60 100 30 70 110 40 80 120
C = num2cell(A,1)
C =1×3×2 cell array(:,:,1) = [4×1 double] [4×1 double] [4×1 double] (:,:,2) = [4×1 double] [4×1 double] [4×1 double]
每个4-by-1vector contains elements from along thefirstdimension ofA
:
C{1}
ans =1 2 3 4
Create a 4-by-1-by-2 cell array of 1-by-3 numeric arrays.
C = num2cell(A,2)
C =4×1×2 cell array(:,:,1) = [1×3 double] [1×3 double] [1×3 double] [1×3 double] (:,:,2) = [1×3 double] [1×3 double] [1×3 double] [1×3 double]
Each 1-by-3 row vector contains elements from along theseconddimension ofA
:
C{1}
ans =1 5 9
Finally, create a 4-by-3 cell array of 1-by-1-by-2 numeric arrays.
C = num2cell(A,3)
C =4×3 cell array[1×1×2双][1×1×2双][1×1×2双][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 thethirddimension ofA
:
C{1}
ans = (:,:,1) = 1 (:,:,2) = 10
Combine Across Multiple Dimensions
Create a cell array by combining elements into numeric arrays along several dimensions.
A = reshape(1:12,4,3); A(:,:,2) = A*10
A = (:,:,1) = 1 5 9 2 6 10 3 7 11 4 8 12 (:,:,2) = 10 50 90 20 60 100 30 70 110 40 80 120
c = num2cell(A,[1 3])
c =1×3 cell array[4×1×2 double] [4×1×2 double] [4×1×2 double]
每个4-by-1-by-2 array contains elements from along the first and third dimension ofA
:
c{1}
ans = (:,:,1) = 1 2 3 4 (:,:,2) = 10 20 30 40
c = num2cell(A,[2 3])
c =4×1 cell array[1×3×2 double] [1×3×2 double] [1×3×2 double] [1×3×2 double]
Input Arguments
A
— Input
any type of multidimensional array
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
dim
— Dimension ofA
positive integer | positive vector of integers
Dimension ofA
, specified as a positive integer or a vector of positive integers.dim
must be between 1 andndims
(A
).
Elements need not be in numeric order. However,num2cell
permutes the dimensions of the arrays in each cell ofC
to match the order of the specified dimensions.
Data Types:double
|single
|int8
|int16
|int32
|int64
|uint8
|uint16
|uint32
|uint64
Output Arguments
C
— Resulting array
cell array
Resulting array, returned as a cell array. The size ofC
depends on the size ofA
and the values ofdim
.
If
dim
is not specified, thenC
is the same size asA
.If
dim
is a scalar, thenC
containsnumel(A)/size(A,dim)
cells. Ifdim
is 1 or 2, then each cell contains a column or row vector, respectively. Ifdim
> 2, then each cell contains an array whosedim
th dimensional length issize(A,dim)
, and whose other dimensions are all singletons.For example, given a 4-by-7-by-3 array,
A
, this figure shows hownum2cell
creates cells corresponding todim
values of1
,2
, and3
.If
dim
is a vector containingN
values, thenC
hasnumel(A)/prod([size(A,dim(1)),...,size(A,vdim(N))])
cells. Each cell contains an array whosedim
(i)
th dimension has a length ofsize(A,dim(i))
and whose other dimensions are singletons.For example, given a 4-by-7-by-3 array, you can specify
dim
as an positive integer vector to create cell arrays of different dimensions.
Data Types:cell
Introduced before R2006a
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select:.
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina(Español)
- Canada(English)
- United States(English)
Europe
- Belgium(English)
- Denmark(English)
- Deutschland(Deutsch)
- España(Español)
- Finland(English)
- France(Français)
- Ireland(English)
- Italia(Italiano)
- Luxembourg(English)
- Netherlands(English)
- Norway(English)
- Österreich(Deutsch)
- Portugal(English)
- Sweden(English)
- Switzerland
- United Kingdom(English)