细胞间的相关系数

时间:2012-04-03 11:58:35

标签: matlab correlation

我的数据集存储方式与以下示例类似:

clear all
Year = cell(1,4);
Year{1} = {'Y2007','Y2008','Y2009','Y2010','Y2011'};
Year{2} = {'Y2005','Y2006','Y2007','Y2008','Y2009'};
Year{3} = {'Y2009','Y2010','Y2011'};
Year{4} = {'Y2007','Y2008','Y2009','Y2010','Y2011'};

data = cell(1,4);
data{1} = {rand(26,1),rand(26,1),rand(26,1),rand(26,1),rand(26,1)};
data{2} = {rand(26,1),rand(26,1),rand(26,1),rand(26,1),rand(26,1)};
data{3} = {rand(26,1),rand(26,1),rand(26,1)};
data{4} = {rand(26,1),rand(26,1),rand(26,1),rand(26,1),rand(26,1)};

“年份”中的每个单元格表示收集“数据”中每次测量的时间。例如,年份中的第一个单元格(“年{1}”)包含收集“数据{1}”中每个度量的年份,以便在“Y2007”中收集数据{1} {1},数据{1 'Y2008'中的{2} ...等等

我现在正试图找出每个测量与来自其他位置的相应(同年)测量之间的相关性。例如,对于'Y2007'年,我想找到数据{1} {1}和数据{2} {3}之间的相关性,然后是数据{1} {1}和数据{4} {1},以及然后是剩余年份的数据{2} {3}和数据{4} {1}等等。

我知道应该使用corrcoef命令来计算相关性,但我似乎无法进入可能的阶段。任何建议将不胜感激。

1 个答案:

答案 0 :(得分:0)

我假设每个细胞只出现一年一次。这是我最终得到的代码(请参阅解释说明):

yu = unique([Year{:}]); %# cell array of unique year across all cells
cc = cell(size(yu)); %# cell array for each year
for y = 1:numel(yu)
    %# which cells have y-th year
    yuidx = cellfun(@(x) find(ismember(x,yu{y})), Year, 'UniformOutput',0);
    yidx = find(cellfun(@(x) ~isempty(x), yuidx, 'UniformOutput',1));
    if numel(yidx) <= 1
        continue
    end
    %# find indices for y-th year in each cell
    yidx2 = cell2mat(yuidx(yidx));
    %# fill matrix to calculate correlation
    ydata = zeros(26,numel(yidx)); 
    for k = 1:numel(yidx)
        ydata(:,k) = data{yidx(k)}{yidx2(k)};
    end
    %# calculate correlation coefficients
    cc{y} = corr(ydata);
end

yu将列出所有年份。 cc将包含每年的相关矩阵。如果你愿意,你也可以保留yidx(如果你使它成为一个单元格数组,相应地更改代码)。

相关问题