如何在Matlab中的多维数组中应用corr2函数?

时间:2014-10-23 09:20:46

标签: matlab multidimensional-array vectorization

假设我有两个矩阵A和B

A = rand(4,5,3);
B = rand(4,5,6)

我想应用函数'corr2'来计算相关系数。

corr2(A(:,:,1),B(:,:,1))
corr2(A(:,:,1),B(:,:,2))
corr2(A(:,:,1),B(:,:,3))
...
corr2(A(:,:,1),B(:,:,6))
...
corr2(A(:,:,2),B(:,:,1))
corr2(A(:,:,2),B(:,:,2))
...
corr2(A(:,:,3),B(:,:,6))

如何避免使用循环来创建这样的矢量化?

2 个答案:

答案 0 :(得分:7)

入侵corr2的m文件以创建用于处理3D阵列的自定义矢量化版本。这里提出了两种方法bsxfun(当然!)

方法#1

szA = size(A);
szB = size(B);

a1 = bsxfun(@minus,A,mean(mean(A)));
b1 = bsxfun(@minus,B,mean(mean(B)));

sa1 = sum(sum(a1.*a1));
sb1 = sum(sum(b1.*b1));

v1 = reshape(b1,[],szB(3)).'*reshape(a1,[],szA(3));
v2 = sqrt(sb1(:)*sa1(:).');

corr3_out = v1./v2; %// desired output

corr3_outcorr2A的所有3D切片之间存储B个结果。

因此,对于A = rand(4,5,3), B = rand(4,5,6),我们将corr3_out作为6x3数组。

方法#2

通过使用sum代替保存对meanreshape的少量来电的方法略有不同 -

szA = size(A);
szB = size(B);
dim12 = szA(1)*szA(2);

a1 = bsxfun(@minus,A,mean(reshape(A,dim12,1,[])));
b1 = bsxfun(@minus,B,mean(reshape(B,dim12,1,[])));

v1 = reshape(b1,[],szB(3)).'*reshape(a1,[],szA(3));
v2 = sqrt(sum(reshape(b1.*b1,dim12,[])).'*sum(reshape(a1.*a1,dim12,[])));

corr3_out = v1./v2; %// desired output

基准

基准代码 -

%// Create random input arrays
N = 55; %// datasize scaling factor
A = rand(4*N,5*N,3*N);
B = rand(4*N,5*N,6*N);

%// Warm up tic/toc
for k = 1:50000
    tic(); elapsed = toc(); 
end

%// Run vectorized and loopy approach codes on the input arrays

%// 1. Vectorized approach
%//... solution code (Approach #2) posted earlier
%// clear variables used

%// 2. Loopy approach
tic
s_A=size(A,3);
s_B=size(B,3);
out1 = zeros(s_B,s_A);
for ii=1:s_A
    for jj=1:s_B
        out1(jj,ii)=corr2(A(:,:,ii),B(:,:,jj));
    end
end
toc

结果 -

-------------------------- With BSXFUN vectorized solution 
Elapsed time is 1.231230 seconds.
-------------------------- With loopy approach
Elapsed time is 139.934719 seconds.

MATLAB-JIT爱好者在这里展示了一些爱!:)

答案 1 :(得分:1)

一些例子,但没有一个比循环好。正如Divakar在下面的评论中所说,这不是一个矢量化的解决方案。

CODE:

A = rand(4,5,1000);
B = rand(4,5,200);
s_A=size(A,3);
s_B=size(B,3);

%%% option 1
tic
corr_AB=cell2mat(arrayfun(@(indx1) arrayfun(@(indx2) corr2(A(:,:,indx1),B(:,:,indx2)),1:s_B),1:s_A,'UniformOutput',false));
toc

%%% option 2
tic
indx1=repmat(1:s_A,s_B,1);
indx1=indx1(:);
indx2=repmat(1:s_B,1,s_A);
indx2=indx2(:);
indx=[indx1,indx2];

corr_AB=arrayfun(@(i) corr2(A(:,:,indx(i,1)),B(:,:,indx(i,2))),1:size(indx,1));
toc

%%% option 3
tic
a=1;
for i=1:s_A
    for j=1:s_B
        corr_AB(a)=corr2(A(:,:,i),B(:,:,j));
        a=a+1;
    end
end
toc

输出:

Elapsed time is 9.655696 seconds.
Elapsed time is 9.398979 seconds.
Elapsed time is 8.489744 seconds.