diff general/cellutils/cellzip.m @ 4:e44f49929e56

Adding reorganised general toolbox, now in several subdirectories.
author samer
date Sat, 12 Jan 2013 19:21:22 +0000
parents
children 47cb292350f3
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/general/cellutils/cellzip.m	Sat Jan 12 19:21:22 2013 +0000
@@ -0,0 +1,23 @@
+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}=feval(fn,args{:});
+end
+
+