% Do corr on all elements of a cell array (correlation coefficient)

function s = cellcorr(cellarray1,cellarray2)

if ~all(size(cellarray1)==size(cellarray2))
    error('Both cell arrays must be the same size');
end

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

if iscell(cellarray1{1,1})
    for i=1:size(cellarray1,1)
        for j=1:size(cellarray1,2)
            s{i,j} = cellcorr(cellarray1{i,j},cellarray2{i,j});
        end
    end
    return;
end

for i=1:size(cellarray1,1)
 for j=1:size(cellarray1,2)
     if size(cellarray1,1)==1
         thecorr = corr(cellarray1{i,j}',cellarray2{i,j}');
     else
         thecorr = corr(cellarray1{i,j},cellarray2{i,j});
     end
     if length(thecorr)==1
         s(i,j) = thecorr;
     else
         s{i,j} = thecorr;
     end
 end
end
