%MAPCELL cellfun, but fun can be anything feval'able
% function [D] = mapcell( fun, C )
% overview:
% for each element of C, fun is evaluated with that element as
% the argument. Optional arguments passed to mapcell are also
% passed along to fun. The code is shorter than the english to
% describe it:
% D = cell(size(C));
% for k=1:prod(size(C))
% D{k} = feval( fun, C{k}, varargin{:} );
% end
% If all of the outputs D are exactly 1-by-1, D is converted to
% from a cell array to an array.
% inputs:
% fun: some feval'able form, like a function handle or an inline
% object, or the name of function. note that all of the
% cellfun strings work identically: 'isreal', 'isempty',
% 'islogical', 'length', 'ndims', 'prodofsize', 'size',
% 'isclass'
% C: the cell array in consideration
% outputs:
% D: a cell or double array the same size as C.
% S: if D is a cell array, S is prod(size(D))-by-
% max(mapcell(@ndims,D)) where the rows are the size of the
% corresponding element in D. if D is a regular array S is
% ones(size(D)).
% Copyright 2001-2003 Daniel Simms <dsimms@dsimms.com>
% $Revision$ $Date$
function [D,S] = mapcell( fun, C, varargin )
D = cell(size(C));
for k=1:prod(size(C))
D{k} = feval( fun, C{k}, varargin{:} );
end
n = zeros(size(D));
for k=1:prod(size(D));
n(k) = ndims(D{k});
end
S = ones(prod(size(D)),max(n));
for k=1:prod(size(D));
S(k,1:n(k)) = size(D{k});
end
if( all(1 == prod(S,2)) )
D = reshape( cat(1, D{:}), size(C) );
end
return;
% dummy; so that all of the strings defined by
% cellfun will work.
function S = prodofsize(A)
S = prod(size(A));
return;