| Contents | Index |
C = num2cell(A)
C = num2cell(A,dim)
C = num2cell(A,[dim1,...,dimN])
C = num2cell(A) converts array A into cell array C by placing each element of A into a separate cell in C.
C = num2cell(A,dim) creates vectors in cells of C that contain elements from the specified dimension dim of A.
C = num2cell(A,[dim1,...,dimN]) creates arrays in cells of C that contain elements from the specified dimensions of A.
To run the examples in this section, first create a three-dimensional numeric array.
a = reshape(1:12,4,3)'; a(:,:,2) = a * 10
a is 3-by-4-by-2 array:
a(:,:,1) =
1 2 3 4
5 6 7 8
9 10 11 12
a(:,:,2) =
10 20 30 40
50 60 70 80
90 100 110 120Convert numeric array a (created in the previous example) to a cell array with the same dimensions.
c = num2cell(a);
c is a 3-by-4-by-2 cell array, where each cell contains a scalar numeric value:
c(:,:,1) =
[1] [ 2] [ 3] [ 4]
[5] [ 6] [ 7] [ 8]
[9] [10] [11] [12]
c(:,:,2) =
[10] [ 20] [ 30] [ 40]
[50] [ 60] [ 70] [ 80]
[90] [100] [110] [120]Combine the elements from the first dimension of array a (created in a previous example) into vectors within cells of cell array c2.
c2 = num2cell(a, 1)
Each cell of c2 contains a 3-by-1 vector:
c2(:,:,1) =
[3x1 double] [3x1 double] [3x1 double] [3x1 double]
c2(:,:,2) =
[3x1 double] [3x1 double] [3x1 double] [3x1 double]where, for example, c2{1} returns
ans =
1
5
9Combine the elements in the first and second dimensions of array a (created in a previous example) into arrays within cells of cell array c3.
c3 = num2cell(a, [1, 2])
Each cell of c3 contains a 3-by-4 numeric array:
c3(:,:,1) =
[3x4 double]
c3(:,:,2) =
[3x4 double]where, for example, c3{1} returns
ans =
1 2 3 4
5 6 7 8
9 10 11 12| © 1984-2012- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |