Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to compute the autocorrelation matrix of a random vector in matlab?

1,262 views
Skip to first unread message

Thinking

unread,
Apr 20, 2009, 7:46:48 AM4/20/09
to
I want to use matlab to compute the autocorrelaton matrix of a random
vector. Here is the code I have written. Is it correct?
clc;
vec_num = 1000;
x = zeros(8,vec_num);
for i=1:vec_num
x(:,i) = round(randn(8,1)*10);
end

total_matrix = zeros(8,8);
for i=1:vec_num
for j=1:vec_num
total_matrix = total_matrix + x(:,i)*x(:,j)';
end
end
total_matrix = total_matrix / (vec_num*vec_num);

%%
total_matrix is the autocorrelation matrix.

Yi Cao

unread,
Apr 20, 2009, 8:13:01 AM4/20/09
to
Thinking <zhihan...@gmail.com> wrote in message <719fb4e3-81ab-4752...@l16g2000pra.googlegroups.com>...

Sounds like you are looking for correlation coefficients. If so, try 'help corrcoef'. Otherwise, try 'help corrmtx' if you have signal processing toolbox.

Yi

Roger Stafford

unread,
Apr 20, 2009, 9:10:03 AM4/20/09
to

You are attempting to compute a sample correlation matrix, but you haven't subtracted out the sample means. Of course, using 'randn' should give a theoretical mean of zero, but that wouldn't be true of an actual sample in general. Also theoretically there would be no cross correlation, but again that will not be strictly true of a sample.

If you use the matlab 'corrcoef' function beware that your matrix x is the transpose of what it expects. 'corrcoef' expects the rows to be observations (eight variable values) and the columns to be variables (1000 each,) so your x would give a 1000 x 1000 matrix unless you did a transpose.

Roger Stafford

Roger Stafford

unread,
Apr 20, 2009, 10:16:01 AM4/20/09
to
"Roger Stafford" <ellieandr...@mindspring.com.invalid> wrote in message <gshs7b$bp6$1...@fred.mathworks.com>...
> You are attempting to compute a sample correlation matrix, but you haven't subtracted out the sample means. ......

Actually there is more wrong with your calculations than I stated. 1) There should only be a single, not a double, summation over the columns of x. 2) You are dividing by vec_num instead of vec_num-1 for unbiased values. 3) Finally you haven't divided by the standard deviations so as to get a diagonal of all ones, so you are just getting covariances, not correlations.

Roger Stafford

0 new messages