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

2 views (last 30 days)
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
Fede C 2018 London on 4 Jul 2021
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