移动相关
Moving 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 循环,但有更聪明的方法吗?
我对correlation的定义和Matlab corr的定义是一样的。我想要做的就是获得它的移动 window。
您可以使用 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;
我有两个数组:
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 循环,但有更聪明的方法吗?
我对correlation的定义和Matlab corr的定义是一样的。我想要做的就是获得它的移动 window。
您可以使用 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;