Home > demo2 > foreach.m

foreach

PURPOSE ^

foreach - apply function to all elements in a cell array

SYNOPSIS ^

function B=foreach(fun, A, depth, varargin)

DESCRIPTION ^

 foreach - apply function to all elements in a cell array
 B=foreach(fun, A, depth, varargin)
  depth : max depth of evaluation for nested cell matrices
          (default inf)

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function B=foreach(fun, A, depth, varargin)
0002 % foreach - apply function to all elements in a cell array
0003 % B=foreach(fun, A, depth, varargin)
0004 %  depth : max depth of evaluation for nested cell matrices
0005 %          (default inf)
0006 
0007 
0008 if ~exist('depth','var') | isempty(depth)
0009   depth = inf;
0010 end
0011 
0012 n=prod(size(A));
0013 B=cell(size(A));
0014 
0015 for i=1:n
0016   if ~iscell(A{i}) | depth == 0
0017     B{i} = feval(fun, A{i}, varargin{:});
0018   else
0019     B{i} = foreach(fun, A{i}, depth-1, varargin{:});
0020   end
0021 end

Generated on Sat 22-Aug-2009 22:15:36 by m2html © 2003