Skip to content

Commit

Permalink
Adding private num2cell.
Browse files Browse the repository at this point in the history
  • Loading branch information
Thurston B committed Aug 28, 2024
1 parent a67f18a commit a792818
Showing 1 changed file with 74 additions and 0 deletions.
74 changes: 74 additions & 0 deletions kern/private/num2cell.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
function c = num2cell(a,dims)
%NUM2CELL Convert numeric array into cell array.
% C = NUM2CELL(A) converts numeric array A into cell array C by placing
% each element of A into a separate cell in C. The output array has the
% same size and dimensions as the input array. Each cell in C contains
% the same numeric value as its respective element in A.
%
% C = NUM2CELL(A, DIM) converts numeric array A into a cell array of
% numeric vectors, the dimensions of which depend on the value of the DIM
% argument. Return value C contains NUMEL(A)/SIZE(A,DIM) vectors, each of
% length SIZE(A, DIM). The DIM input must be an integer with a value from
% NDIMS(A) to 1.
%
% C = NUM2CELL(A, [DIM1, DIM2, ...]) converts numeric array A into a cell
% array of numeric arrays, the dimensions of which depend on the values
% of arguments [DIM1, DIM2, ...]. Given the variables X and Y, where
% X=SIZE(A,DIM1) and Y=SIZE(A,DIM2), return value C contains
% NUMEL(A)/PROD(X,Y,...) arrays, each of size X-by-Y-by-.... All DIMn
% inputs must be an integer with a value from NDIMS(A) to 1.
%
% NUM2CELL works for all array types.
%
% Use CELL2MAT or CAT(DIM,C{:}) to convert back.
%
% See also MAT2CELL, CELL2MAT

% Clay M. Thompson 3-15-94
% Copyright 1984-2022 The MathWorks, Inc.

narginchk(1,2);
if nargin==1
if isa(a, 'function_handle')
c = {a};
return
end
c = cell(size(a));
for i=1:numel(a)
c{i} = a(i);
end
return
end

% Size of input array
siz = [size(a),ones(1,max(dims)-ndims(a))];

% Create remaining dimensions vector
rdims = 1:max(ndims(a),max(dims));
rdims(dims) = []; % Remaining dims

% Size of extracted subarray
bsize(sort(dims)) = siz(dims);
bsize(rdims) = 1; % Set remaining dimensions to 1

% Size of output cell
csize = siz;
csize(dims) = 1; % Set selected dimensions to 1
c = cell(csize);

% Permute A so that requested dims are the first few dimensions
a = permute(a,[dims rdims]);

% Make offset and index into a
offset = prod(bsize);
ndx = 1:prod(bsize);
try
for i=0:prod(csize)-1,
c{i+1} = reshape(a(ndx+i*offset),bsize);
end
catch E
% This should never happen!
error('unexpected:num2cell', ...
'Unexpected error in num2cell. size(a): [%s]; dims: [%s]; offset: %d; ndx: [%s]; i: %d; error: %s', ...
strjoin(string(size(a))), strjoin(string(dims)), offset, strjoin(string(ndx)), i, getReport(E));
end

0 comments on commit a792818

Please sign in to comment.