view general/cellutils/cellzip.m @ 38:9d24b616bb06

Added function algebra.
author samer
date Tue, 29 Jan 2013 15:59:01 +0000
parents 47cb292350f3
children
line wrap: on
line source
function Y=cellzip(fn,varargin)
% cellzip - Zip cell arrays with a function 
%
% cellzip :: (
% 	(D1,...,Dn)->R	                  ~ function of n arguments,
% 	 {[size]->D1}, ..., {[size]->Dn}	~ n cell arrays of appropriate types
% ) -> {[size]->R}                	~ cell array of results

% preallocate to fix size
sizes=cellmap(@size,varargin);
sz=sizes{1};
for i=1:nargin-1
	if ~all(sizes{i}==sz), error('Sizes do not match'); end 
end

Y=cell(sz);

for i=1:prod(sz)
	args=cellmap(@(c)c{i},varargin);
	Y{i}=fn(args{:});
end