Generating covariance from matrix

조회 수: 4 (최근 30일)
Ryan Chow
Ryan Chow 2020년 1월 24일
댓글: Walter Roberson 2020년 1월 24일
I have the data y as follows and I am trying to find the covariance between the 1st row and 2nd row, 2nd row and 3rd row, 3rd row and 4th row, so on. I don't seem to be able to save the values into a matrix either and would appreciate any hints. Thank you!
y = rand(20,1)
for i = 1:20
autocov=cov(y(i:19),y(i+1:20))
end

채택된 답변

Walter Roberson
Walter Roberson 2020년 1월 24일
편집: Walter Roberson 2020년 1월 24일
N = 20;
y = rand(N,1);
autocov = zeros(2, 2, N);
for i = 1:N
autocov(:, :, i) = cov(y(i:19),y(i+1:20));
end
The result will be a 2 x 2 x 20 array.
  댓글 수: 2
Ryan Chow
Ryan Chow 2020년 1월 24일
I'm guessing the code should be something like (after modification from yours):
N = 20;
y = rand(N,1);
for i = 1:N
autocov = cov(y(i:19),y(i+1:20))
end
What I then get would be 19 of the following:
autocov =
0.0662 -0.0166
-0.0166 0.0630
autocov =
0.0486 -0.0194
-0.0194 0.0666
Is there any way I could write a loop to save those numbers in bold in another vector?
Thanks!
Walter Roberson
Walter Roberson 2020년 1월 24일
N = 20;
y = rand(N,1);
cov12 = zeros(1,N);
for i = 1:N
autocov = cov(y(i:19),y(i+1:20));
cov12(i) = autocov(1,2);
end

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by