移动相关

时间:2015-05-28 12:29:27

标签: matlab correlation

我有两个数组:

A = 1:20
B = 101:120

我想找到移动的相关性。即

CorrMat(1) = corr( 1:15, 101:115 );
CorrMat(2) = corr( 2:16, 102:116 );
CorrMat(3) = corr( 3:17, 103:117 );
CorrMat(4) = corr( 4:18, 104:118 );    

等等。

我可以为此编写一个for循环但是有更聪明的方法吗?

我对相关性的定义与Matlab corr相同。我所要做的就是获得一个移动窗口。

1 个答案:

答案 0 :(得分:3)

您可以使用arrayfun

A = 1:20;     %// First array, same numel() as second
B = 101:120;  %// Second array, same numel() as first
W = 1:15;     %// Window indices, always starting with 1

CorrCell = arrayfun(          ...
  @(k) corr(A(k+W), B(k+W)),  ...
  0:(numel(A)-numel(W)),      ...
  'UniformOutput', false      ...
);

出于对比的目的,我将编写基于循环的实现:

N = numel(A) - numel(W);
CorrLoop = cell(1, N+1);
for k = 0:N
        wnd = k+W;
        CorrLoop{k+1} = corr(A(wnd), B(wnd));
end;