Extract one element from a matrix in a loop and save unto a vector

2 ビュー (過去 30 日間)
Fede C 2018 London
Fede C 2018 London 2021 年 7 月 2 日
Hi there,
I'm trying to store the correlation coefficients between one variable and 6 (respectively) leads and lags of another variable. I'm aware there's ways to go around negative indexing-have seen there's a few questions and answers on that. I'm struggling with something even more basic, though.
I pre-allocate a vector where I want to store results (corcoefvector), but then what I have in the loop doesn't work, because the RHS returns a 2x2 matrix (the matrix of correlation coefficients), and what I need is to extract only one element, that is, one of the off-diagonal values (since it's just two variable at each iteration of the loop), say element (1,2) from the matrix of coefficient at iteration 1 and shove it in position (1,1) of corcoefvector, and keep doing this as j goes from 1 to 6. The size of the LHS is 1-by-1, the size of the RHS is 2-by-2. I'm at a loss as to the best way to proceed. Would highly appreciate some help in getting unstuck
corcoefvector=zeros(1,6);
for j=1:6
corcoefvector(1,j)= corrcoef(filteredcol2,lagmatrix(filteredcol1,j),'rows','complete');
end
Many thanks
  3 件のコメント
Fede C 2018 London
Fede C 2018 London 2021 年 7 月 2 日
Hi Bob,
filteredcol2 and filteredcol1 are two vectors containing the variables. filteredcol1 is the variable that I need to take leads and lags of (here, only lags-as I alluded to earlier I'll have to find a way around negative indexing for the leads), so lagmatrix(filteredcol1,1) would lag filteredcol1 by one period. The input 'complete' tells matlab to compute the correlation coefficients ignoring the row that would have NaN in it).
So, for j=1, I would lag filteredcol1 by 1 period, the function corrcoef would spit out the correlation coefficient between filteredcol2 and filteredcol1 lagged by 1 by way of a 2x2 matrix. I would want to take element (1,2) from that column and store it in the 1st element of vector corcoefvector, and so on.
Fede C 2018 London
Fede C 2018 London 2021 年 7 月 4 日
this does it:
corcoefvector=zeros(1,6);
for j=1:6
tempmat1 = corrcoef(filteredcol2,lagmatrix(filteredcol1,j),'rows','complete');
corcofvector(j) = tempmat1(1,2);
end

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

回答 (0 件)

カテゴリ

Help Center および File ExchangeCorrelation and Convolution についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by