-
Notifications
You must be signed in to change notification settings - Fork 1
/
cellcovold.m
65 lines (54 loc) · 1.45 KB
/
cellcovold.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
function [c] = cellcov(x, y, dim, flag)
% [C] = CELLCOV(X, DIM) computes the covariance, across all cells in x along
% the dimension dim. When there are three inputs, covariance is computed between
% all cells in x and y
%
% X (and Y) should be linear cell-array(s) of matrices for which the size in at
% least one of the dimensions should be the same for all cells
if nargin<4 && iscell(y)
flag = 1;
elseif nargin<4 && isnumeric(y)
flag = dim;
end
if nargin<3 && iscell(y)
scx1 = cellfun('size', x, 1);
scx2 = cellfun('size', x, 2);
if all(scx2==scx2(1)), dim = 2; %let second dimension prevail
elseif all(scx1==scx1(1)), dim = 1;
else error('no dimension to compute covariance for');
end
elseif nargin<=3 && isnumeric(y)
dim = y;
end
if isnumeric(y), y = []; end
nx = size(x);
if ~iscell(x) || length(nx)>2 || all(nx>1),
error('incorrect input for cellcov');
end
if flag,
mx = cellmean(x, 2);
x = cellvecadd(x, -mx);
if ~isempty(y),
my = cellmean(y, 2);
y = cellvecadd(y, -my);
end
end
nx = max(nx);
nsmp = cellfun('size', x, dim);
if isempty(y),
csmp = cellfun(@covc, x, repmat({dim},1,nx), 'UniformOutput', 0);
else
csmp = cellfun(@covc, x, y, repmat({dim},1,nx), 'UniformOutput', 0);
end
nc = size(csmp{1});
c = sum(reshape(cell2mat(csmp), [nc(1) nc(2) nx]), 3)./sum(nsmp);
function [c] = covc(x, y, dim)
if nargin==2,
dim = y;
y = x;
end
if dim==1,
c = x'*y;
elseif dim==2,
c = x*y';
end