Question on covariance matrix loop

6 ビュー (過去 30 日間)
Mike
Mike 2013 年 10 月 30 日
コメント済み: Azzi Abdelmalek 2013 年 10 月 31 日
Really basic question from a beginner- I have a 60,000 by 4 matrix (AB) and want to calculate 2000 covariance matrices ( every 30 rows down, and all four columns).
Do you guys know whats wrong with my logic? The below sample loops the 2nd and third covariance matrices.
for i = 1:2 j=i*31; k= j+30; VarCov(i) = cov(AB( j:k ,:)); end
I get an error message of:
In an assignment A(I) = B, the number of elements in B and
I must be the same.
But doesnt varcov consists of 2 matrices??

回答 (2 件)

Shashank Prasanna
Shashank Prasanna 2013 年 10 月 30 日
If you want to stack 2000 cov matrices then you may want to use 3D matrices,
VarCov(:,:,i) = cov(AB(j:k,:));
Here for each i you will have a 4x4 cov matrix
  1 件のコメント
Shashank Prasanna
Shashank Prasanna 2013 年 10 月 31 日
mean(s,3) will work if you store it this way

サインインしてコメントする。


Azzi Abdelmalek
Azzi Abdelmalek 2013 年 10 月 30 日
a=rand(60000,4);
b=permute(reshape(a',4,30,[]),[2 1 3]);
for k=1:size(b,3)
s{k,1}=cov(b(:,:,k));
end
out=cell2mat(s);
  2 件のコメント
Mike
Mike 2013 年 10 月 31 日
Thanks!- This works great. If I want to create an average of the 2000 matrices (stored as "s"), does mean(s,3) work?
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 10 月 31 日
What is the size of the average result?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by