mat2cell
Convert array to cell array whose cells contain subarrays
Description
C = mat2cell(
divides array A
,dim1Dist,...,dimNDist
)A
into smaller arrays and returns them in cell array
C
. The vectors dim1Dist,...dimNDist
specify how to
divide the rows, the columns, and (when applicable) the higher dimensions of
A
. The smaller arrays in C
can have different sizes.
A
can have any data type.
Examples
Divide Array and Return Subarrays in Cell Array
Create a 5-by-4 numeric array.
A = reshape(1:20,5,4)'
A = 4×5
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
Divide A
into two 2-by-3 and two 2-by-2 subarrays. Return the subarrays in a cell array.
C = mat2cell(A,[2 2],[3 2])
C=2×2 cell array
{2x3 double} {2x2 double}
{2x3 double} {2x2 double}
Display the subarrays in C
using the celldisp
function.
celldisp(C)
C{1,1} = 1 2 3 6 7 8 C{2,1} = 11 12 13 16 17 18 C{1,2} = 4 5 9 10 C{2,2} = 14 15 19 20
Divide Array by Rows
Create an array.
A = reshape(1:20,5,4)'
A = 4×5
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
Divide the rows of A
so that the cell array contains two subarrays. Since the first element of rowDist
is 1
, the first cell of C
contains the first row of A
. The second element of rowDist
is 3
, so the next cell of C
contains the next three rows of A
. The sum of the elements of rowDist
equals the number of rows of A
.
rowDist = [1 3]; C = mat2cell(A,rowDist)
C=2×1 cell array
{[1 2 3 4 5]}
{3x5 double }
Display the subarrays.
celldisp(C)
C{1} = 1 2 3 4 5 C{2} = 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Input Arguments
A
— Input array
array
Input array.
dim1Dist,...,dimNDist
— Distributions of input array elements
numeric vectors
Vectors describing the distributions of input array elements along each dimension, specified as numeric vectors.
For example, if A
is a 60-by-50 array, then you can specify this
argument as [10 20 30],[25 25]
to divide A
as
shown in the code and figure. C
is a cell array that contains the six
subarrays split out of A
.
C = mat2cell(A,[10 20 30],[25 25])
For the K
th dimension of A
, specify the
elements of the corresponding vector dimKDist
so that
sum(dimKDist)
equals the size of the K
th
dimension.
If the K
th dimension of A
has a size of zero,
then specify the corresponding vector dimKDist
as the empty array,
[]
, as shown in the code.
A = rand(3,0,4); C = mat2cell(A,[1 2],[],[2 1 1]);
rowDist
— Distribution by rows
numeric vector
Vector describing the distribution by rows of the input array, specified as a
numeric vector. When you do not specify how to divide A
along any
other dimension, the mat2cell
function returns an
n
-by-1 cell array C
, where n
equals the number of elements in rowDist
.
Each element of rowDist
specifies the number of rows in the
subarray that is in the corresponding cell of C
. The sum of the
elements of rowDist
must equal the number of rows of
A
.
Extended Capabilities
Thread-Based Environment
Run code in the background using MATLAB® backgroundPool
or accelerate code with Parallel Computing Toolbox™ ThreadPool
.
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
Version History
Introduced before R2006a
Open Example
You have a modified version of this example. Do you want to open this example with your edits?
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)