time | Calls | line |
---|
| | 1 | function c = num2cell(a,dims)
|
| | 2 | %NUM2CELL Convert numeric array into cell array.
|
| | 3 | % C = NUM2CELL(A) converts numeric array A into cell array C by placing
|
| | 4 | % each element of A into a separate cell in C. The output array has the
|
| | 5 | % same size and dimensions as the input array. Each cell in C contains
|
| | 6 | % the same numeric value as its respective element in A.
|
| | 7 | %
|
| | 8 | % C = NUM2CELL(A, DIM) converts numeric array A into a cell array of
|
| | 9 | % numeric vectors, the dimensions of which depend on the value of the DIM
|
| | 10 | % argument. Return value C contains NUMEL(A)/SIZE(A,DIM) vectors, each of
|
| | 11 | % length SIZE(A, DIM). The DIM input must be an integer with a value from
|
| | 12 | % NDIMS(A) to 1.
|
| | 13 | %
|
| | 14 | % C = NUM2CELL(A, [DIM1, DIM2, ...]) converts numeric array A into a cell
|
| | 15 | % array of numeric arrays, the dimensions of which depend on the values
|
| | 16 | % of arguments [DIM1, DIM2, ...]. Given the variables X and Y, where
|
| | 17 | % X=SIZE(A,DIM1) and Y=SIZE(A,DIM2), return value C contains
|
| | 18 | % NUMEL(A)/PROD(X,Y,...) arrays, each of size X-by-Y-by-.... All DIMn
|
| | 19 | % inputs must be an integer with a value from NDIMS(A) to 1.
|
| | 20 | %
|
| | 21 | % NUM2CELL works for all array types.
|
| | 22 | %
|
| | 23 | % Use CELL2MAT or CAT(DIM,C{:}) to convert back.
|
| | 24 | %
|
| | 25 | % See also MAT2CELL, CELL2MAT
|
| | 26 |
|
| | 27 | % Clay M. Thompson 3-15-94
|
| | 28 | % Copyright 1984-2012 The MathWorks, Inc.
|
| | 29 |
|
< 0.001 | 6 | 30 | narginchk(1,2);
|
< 0.001 | 6 | 31 | if nargin==1
|
< 0.001 | 6 | 32 | c = cell(size(a));
|
< 0.001 | 6 | 33 | for i=1:numel(a)
|
| | 34 | c{i} = a(i);
|
| | 35 | end
|
< 0.001 | 6 | 36 | return
|
| | 37 | end
|
| | 38 |
|
| | 39 | % Size of input array
|
| | 40 | siz = [size(a),ones(1,max(dims)-ndims(a))];
|
| | 41 |
|
| | 42 | % Create remaining dimensions vector
|
| | 43 | rdims = 1:max(ndims(a),max(dims));
|
| | 44 | rdims(dims) = []; % Remaining dims
|
| | 45 |
|
| | 46 | % Size of extracted subarray
|
| | 47 | bsize(sort(dims)) = siz(dims);
|
| | 48 | bsize(rdims) = 1; % Set remaining dimensions to 1
|
| | 49 |
|
| | 50 | % Size of output cell
|
| | 51 | csize = siz;
|
| | 52 | csize(dims) = 1; % Set selected dimensions to 1
|
| | 53 | c = cell(csize);
|
| | 54 |
|
| | 55 | % Permute A so that requested dims are the first few dimensions
|
| | 56 | a = permute(a,[dims rdims]);
|
| | 57 |
|
| | 58 | % Make offset and index into a
|
| | 59 | offset = prod(bsize);
|
| | 60 | ndx = 1:prod(bsize);
|
| | 61 | for i=0:prod(csize)-1,
|
| | 62 | c{i+1} = reshape(a(ndx+i*offset),bsize);
|
| | 63 | end
|
Other subfunctions in this file are not included in this listing.