问题描述:

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
```

您可能感兴趣的文章：

- php - sum values in a loop if another value is false
- Jquery script behaving differently when unobtrusive than when inside the rails view
- fractals - Replacement for juliamap.googlelabs.com?
- linux - NASM::Can't convert string to int with value greater than 255
- Creating http Client for Apache Camel to call certificate enabled webservice
- php - Reading a specific line from a text file
- clr - Any implementation of an Unrolled Linked List in C#?
- Finding Hudson Log Files
- Forward to a payment-gateway together with POST data using cURL (or any other PHP server side solution)
- WCF in Winforms app - is it always single-threaded?

随机阅读：

**推荐内容**-

**热点内容**-
- php - Reading a specific line from a text file
- clr - Any implementation of an Unrolled Linked List in C#?
- Finding Hudson Log Files
- Forward to a payment-gateway together with POST data using cURL (or any other PHP server side solution)
- WCF in Winforms app - is it always single-threaded?
- git svn - git svn fetch does not fetch a Subversion commit message modified after initial clone
- java me - Why I am getting the bad length exception when I am running this application?
- java - How to get string.format to complain at compile time
- ruby on rails - Trigger observer of parent class on change
- python - Issue with URL pattern in Django with webmonkey tutorial