% Do median on all elements of a cell array

function s = cellmedian(cellarray)

if length(size(cellarray))>2
   error('This only works for cell arrays up to 2D');
end

if iscell(cellarray{1,1})
    for i=1:size(cellarray,1)
        for j=1:size(cellarray,2)
            s{i,j} = cellmedian(cellarray{i,j});
        end
    end
    return;
end

for i=1:size(cellarray,1)
 for j=1:size(cellarray,2)
      themedian = median(cellarray{i,j});
      if length(themedian)==1
 	s(i,j) = themedian;
      else
	s{i,j} = themedian;
      end
 end
end
