view general/funutils/map.m @ 4:e44f49929e56

Adding reorganised general toolbox, now in several subdirectories.
author samer
date Sat, 12 Jan 2013 19:21:22 +0000
parents
children 45aaf9b2d7b0
line wrap: on
line source
function y=map(f,x,dim)
% map - Map function over collection
%
% map :: (A->B), collection(A) -> collection(B).
%
% collection(A) ::= seq A | {D->A} | [D->A].
%
% This implementation handles cell arrays and ordinary arrays.
% Sequences should be taken care of by data/map

if isempty(x)
	y=x;
elseif iscell(x),
	y=cellmap(f,x);
elseif isnumeric(x)
	if nargin<3, y=reshape(f(x),size(x));
	elseif dim==1, y=maprows(f,x);
	elseif dim==2, y=mapcols(f,x);
	else error(sprintf('Cannot map over dimension %d',dim));
	end
else
	error(sprintf('Cannot map over objects of class %s',class(x)));
end