Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/neo4j/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matlab:Xcorr运算不使用for循环_Matlab_Correlation - Fatal编程技术网

Matlab:Xcorr运算不使用for循环

Matlab:Xcorr运算不使用for循环,matlab,correlation,Matlab,Correlation,我有两个矩阵A(2*1600*3)和B(2*1600)。我试图对A中的每一行对B中的每一行执行xcorr操作,并希望将结果存储在矩阵中。目前我正在使用以下代码 for ii=1:3 for jj=1:2 X(ii,jj)=max((xcorr(A(jj,:,ii),B(jj,:)))); end end 因为我使用了两个for循环,它消耗了更多的时间,并且影响了我的整个程序的执行时间,这个程序已经有了一个for循环。如果没有两个for循环,如何将输出存储在矩阵中

我有两个矩阵A(2*1600*3)和B(2*1600)。我试图对A中的每一行对B中的每一行执行xcorr操作,并希望将结果存储在矩阵中。目前我正在使用以下代码

for ii=1:3
    for jj=1:2
       X(ii,jj)=max((xcorr(A(jj,:,ii),B(jj,:)))); 
    end
end
因为我使用了两个for循环,它消耗了更多的时间,并且影响了我的整个程序的执行时间,这个程序已经有了一个for循环。如果没有两个for循环,如何将输出存储在矩阵中

同时,我还使用cellfun为输出矩阵的一列尝试了上述代码

`cellfun(@(x) max(xcorr(x, B(1,:))), A, 'UniformOutput', false);`
根据我的观察,for循环比cellfun快得多。 执行时间:
For循环:两列矩阵输出为2.4秒。Cellfun:一列矩阵输出为2.6秒。

您可以使用
fft
轻松完成此操作。互相关与卷积非常相似:

% Compute the size of the cross-correlation.
N = size(A,2) + size(B,2) - 1;
% Do correlation using FFT.  We have to flip one of the inputs.
% If A and B are both symmetric, you might want to add the 'symmetric' flag to ifft
Y = ifft(fft(A,N,2) .* fft(flip(B,2),N,2), N, 2);
% Squeeze out the second dimension and transpose so it matches your size and shape.
Y = squeeze(max(Y, [], 2))'

嗨,Sardar,谢谢你的回复,因为矩阵A和B的维数不一样,所以它也需要一个for循环。是的,这个例子使用了R2016b的隐式标量展开。如果您有早期版本的MATLAB,可以在第二行中使用
bsxfun
Y=ifft(bsxfun(@times,fft(A,N,2)fft(flip(B,2),N,2),N,2))Hi-CKT,我在尝试执行上述命令时收到以下错误消息,“未定义函数'fft'用于'cell'类型的输入参数”。Hi-CKT,我的坏消息,我对A和B使用了错误的值。无论如何,上面的行不起作用。您在第一次fft后漏掉了一个逗号,并且第二次fft不一致。对不起;应该读取
Y=ifft(bsxfun(@times,fft(A,N,2),fft(flip(B,2),N,2)),N,2)。同样,假设
size(A)=[2 1600 3]
size(B)=[2 1600]