[英]Matlab:Xcorr operation without using for loops
我有兩個矩陣A(2 * 1600 * 3)和B(2 * 1600)。 我正在嘗試針對B中的每一行對A中的每一行執行xcorr操作,並將結果存儲在Matrix中。 目前,我正在使用以下代碼。
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快得多。 執行時間:對於循環:兩列矩陣輸出為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))'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.