【问题标题】:MATLAB correlation loop excluding specific column排除特定列的 MATLAB 相关循环
【发布时间】:2013-05-02 13:34:02
【问题描述】:

我正在尝试编写代码来对某些数据执行关联,但每次迭代都要从计算中排除一个特定列。 A 是 1000x60x5 矩阵,B 是 1000x1 向量。 目前我有

out(60,5)= zeros;  % preallocate for loop output


for ques=1:size(A,2) 
    for rep=1:size(A,3)
        out(ques,rep) = corr(A(:,[(1:ques-1):(ques+1:end)],rep),B(:),...
        'rows','pairwise','Type','Spearman');
    end    
end

我是否可以指定(而不是 [(1:ques-1):(ques+1:end)])从计算中排除 ques 列?

【问题讨论】:

  • 你的意思是[(1:ques-1):(ques+1:end)]还是[(1:ques-1),(ques+1:end)]
  • 前者。由于 A 是 3 维的,并且维度被指定为 A(:,Z,rep),其中 Z 表示范围 1:end,但缺少 ques。如果这有意义吗?我可能解释得不是很好。
  • 我想你的意思是后者。在Matlab中比较这些:[(1:19):(21:60)][(1:19),(21:60)],前者很简单1:21,后者是1:1921:60的串联,我认为你的意思是

标签: matlab indexing correlation


【解决方案1】:

我假设您处理 3rd 维度的方式与您预期的一样。我认为您所做的很好,但是当ques == 1ques == size(A,2) 像您一样时,这是一个不会出错的替代方法。不利的一面是,它实际上可能比你的方法慢,我还没有测试过。

out(59,60,5)= zeros;  % preallocate for loop output


for ques=1:size(A,2) 
    for rep=1:size(A,3)
        cols = 1:size(A,2);
        cols(ques) = [];
        out(:,ques,rep) = corr(A(:, cols, rep),B,...
        'rows','pairwise','Type','Spearman');
    end    
end

【讨论】:

  • 嗯...不幸的是我现在得到了可怕的??? Subscripted assignment dimension mismatch. 错误...
  • @8eastFromThe3ast 嗯......你在哪一行得到错误?那时每个下标的值是多少?
  • 函数行出错 (out...)。我现在不在工作电脑旁,但有机会会检查下标值
  • @8eastFromThe3ast 对我来说没有意义,你确定Aout 的尺寸吗?也许将out 声明为out(size(A,2),size(A,3))= zeros; 以确保您保持一致?
  • 是的,绝对确定尺寸...即使按照您的建议。
【解决方案2】:

我真正想做的是这个

out= NaN(1000,60,5); % preallocate for speed
for part = 1:size(A,1) % loop for dim 1
    for ques = 1:size(A,2) % loop for dim 2
        for rep = 1:size(A,3) % loop for dim 3
            cols = 1:size(A,2);
            cols(ques)=[]; 
            out(part,ques,rep) = nanmean(A(part,cols,rep));   % exclusive means         
        end
    end
end

out=nanmean(out,3); % collapse third dimension

final = corr(out,B,'rows','pairwise','type','Spearman'); % do the correlation

使用A的每一行依次排除每一列的平均值,跨第三维折叠,然后与B相关。

【讨论】:

    猜你喜欢
    • 2020-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-29
    • 2019-10-23
    相关资源
    最近更新 更多