问题描述:

I have a question on `xcorr`

function in MATLAB.

Currently this function can calculate the autocorrelation of a matrix, but cannot calculate the cross-correlation of 2 matrices:

`A=[1,2;3,4];`

B=[5,6;7,8];

xcorr(A); %% Possible

xcorr(A,A); %% Not Possible

xcorr(A,B); %% Not Possible

Are you aware of any workaround to do this, but without using a `for`

loop?

`xcorr`

has essentially two syntaxes.

```
c = xcorr(x, y)
```

computes the cross-correlation function between two scalar signals (given as vectors), and

```
c = xcorr(x)
```

computes the auto-correlation function of a signal if `x`

is a vector, and the auto- and cross-correlation functions between all columns of `x`

if it is a matrix. If `x`

is of size `n`

x `p`

, then `c`

is of size `2*n-1`

x `p^2`

.

When you write

```
c = xcorr(x, y);
```

with *two matrices* `x`

and `y`

, I assume you want the cross-correlation functions between all signals in `x`

with all signals in `y`

. `xcorr`

can't do this out of the box. However, if the two matrices both have `n`

rows, you can write

```
c = xcorr([x, y]);
```

to get the auto- and cross-correlation functions between all signals that are in `x`

*or* `y`

. `c`

is of size `2*n-1`

x `(p1+p2)^2`

, where `p1`

and `p2`

are the numbers of signals (columns) in the two matrices. You can then reshape and truncate the result:

```
c = reshape(c, 2*n-1, p1+p2, p1+p2);
c = c(:, 1 : p1, p1+1 : end);
```

The result is a three-dimensional matrix where the first dimension corresponds to the lag, the second enumerates the signals in `x`

and the third enumerates the signals in `y`

; its size is `2*n-1*`

x `p1`

x `p2`

.

To handle cross correlation between matrices just use the 2d version of it: xcorr2.

So to calculate autocorrelation just do

```
xcorr2(A)
```

while to find the cross correlation of two matrices

```
xcorr2(A,B)
```

For example, with your A,B the result is:

```
8 23 14
30 70 38
18 39 20
```