Cross correlation of several rows within a matrix

4 ビュー (過去 30 日間)
Ben
Ben 2020 年 5 月 6 日
回答済み: Maneet Kaur Bagga 2024 年 10 月 10 日
Hola!
I have a matrix of 10 column, each column has measurements at different times. I have a second matrix also of 10 columns, each consisting of measurements at different times.
I want to carry out cross correlations of every combination within this matrix. Example, xcorr row 1 matrix 1 with row 1 of matrix 2, then row 1 of matrix 1 of row 2 of matrix 2 and so on. I would like correlations and time lag for each and every combination of columns.
I currently do this, but it means I have to do each row seperatly which is very time consuming.
[xc,lags] = xcorr(matrix1(:,1),matrix(:,2));
[~,I] = max(abs(xc));figure;
plot(lags,xc); legend(sprintf('Time delay %d',lags(I))); title('Column 1 matrix 1 xcorr with Column 2 matrix 2')
Gracias todos, Ben :)

回答 (1 件)

Maneet Kaur Bagga
Maneet Kaur Bagga 2024 年 10 月 10 日
Hi,
As per my understanding you are performing the cross-correlation for each coloumn pair manually for two matrices, for which the "xcorr" function is running separately for each pair of coloumns. This process is time consuming, given the large number of column combinations (10 in each matrix).
Unfortunately, the "xcorr" function cannot be vectorized for multiple column operations to run at once. To reduce the time overhead, instead of plotting each combination in real time you can store the data and plot it after all the computations are completed.
Also, as a possible workaround you can use the "Parallel Computing Toolbox" to parallelize the loops using "parfor" to speed up the cross-correlation computations. Please refer to the following code snippet for better understanding.
parfor i = 1:numColumns
for j = 1:numColumns
[xc, lags] = xcorr(matrix1(:, i), matrix2(:, j));
[~, I] = max(abs(xc));
xcorrResults{i, j} = xc;
lagResults{i, j} = lags;
maxLagResults(i, j) = lags(I);
end
end
Hope this helps!

カテゴリ

Help Center および File ExchangeCorrelation and Convolution についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by