So I'm getting myelf familiar with correlation coeffiecient for a future project. I have a couple objectives I wanted to understand: corrcoef as a whole, and then corrcoef through time (meaning seeing a 'live' corrcoef as the data changes).

So far I made this that works as I wanted:

rand_1 = randi([0 5],1,20);

rand_2 = randi([0 5],1,20);

rand_3 = randi([0 5],1,20);

r_1 = corrcoef(rand_1,rand_2);

R_1 = r_1(2,1)

r_2 = corrcoef(rand_1,rand_3);

R_2 = r_2(2,1)

r_3 = corrcoef(rand_2,rand_3);

R_3 = r_3(2,1)

x = [1:20];

figure

subplot(3,2,[1,2])

plot(x,rand_1,'r',x,rand_2,'b')

title(['Random 1: between 1 & 2, C.C. = ',num2str(R_1),''])

axis([1 20 -1 6])

subplot(3,2,[3,4])

plot(x,rand_1,'r',x,rand_3,'g')

title(['Random 2: between 1 & 3, C.C. = ',num2str(R_2),''])

axis([1 20 -1 6])

subplot(3,2,[5,6])

plot(x,rand_2,'b',x,rand_3,'g')

title(['Random 3: between 2 & 3, C.C. = ',num2str(R_3),''])

axis([1 20 -1 6])

Now I wanted to make something similar for the time series but I'm having some trouble understanding where I am going wrong and how to continue:

L = 20;

rand2_1 = randi([0 5],1,L);

rand2_2 = randi([0 5],1,L);

rand2_3 = randi([0 5],1,L);

K = 2*L;

r2_1 = zeros(2,K); r2_2 = zeros(2,K); r2_3 = zeros(2,K);

for t = 1:1:L

j = 2*t-1;

r2_1(1:2,j:j+1) = corrcoef(rand2_1([t t+1]),rand2_2([t t+1]));

end

I understood that depending on the size of the matrix/data given to corrcoef it would give a different size matrix as an answer (for example a 2x2 in the initial example code's case), but I have not been able to figure that out for this second part. Perhaps I'm extracting the answer wrongly from corrcoef, or perhaps my matrix manipulation is simply wrong.

Anyways, any help would be very much appreciated! Thanks