Documentation

mat2cell

转换array to cell array with potentially different sized cells

Syntax

C = mat2cell(A,dim1Dist,...,dimNDist)
C = mat2cell(A,rowDist)

Description

C= mat2cell(A,dim1Dist,...,dimNDist)divides arrayA成更小的数组单元阵列中C. Vectorsdim1Dist,...dimNDistspecify how to divide the rows, columns, and (when applicable) higher dimensions ofA.

C= mat2cell(A,rowDist)divides arrayAinto ann-by-1 cell arrayC, wheren == numel(rowDist).

Input Arguments

A

Any type of array.

dim1Dist,...,dimNDist

Numeric vectors that describe how to divide each dimension ofA. For example, this command

c = mat2cell(x, [10, 20, 30], [25, 25])

divides a 60-by-50 array into six arrays contained in a cell array.

For thekth dimension,sum(dimkDist) == size(A,k).

If thekth dimension ofAis zero, set the correspondingdimkDistto the empty array,[]. For example,

a = rand(3, 0, 4); c = mat2cell(a, [1, 2], [], [2, 1, 1]);

rowDist

Numeric vector that describes how to divide the rows ofA. When you do not specify distributions for any other dimension, themat2cellfunction creates ann-by-1 cell arrayC, wheren == numel(rowDist).

Output Arguments

C

Cell array. Thekth dimension of arrayCis given bysize(C,k) == numel(dimkDist). Thekth dimension of theith cell ofCis given bysize(C{i},k) == dimkDist(i).

Examples

Divide the 5-by-4 matrixXinto 2-by-3 and 2-by-2 matrices contained in a cell array.

X = reshape(1:20,5,4)' C = mat2cell(X, [2 2], [3 2]) celldisp(C)

This code returns

X = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 C = [2x3 double] [2x2 double] [2x3 double] [2x2 double] 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

DivideX(created in the previous example) into a 2-by-1 cell array.

C = mat2cell(X, [1 3]) celldisp(C)

This code returns

C = [1x5 double] [3x5 double] C{1} = 1 2 3 4 5 C{2} = 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

See Also

|

Introduced before R2006a

Was this topic helpful?