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

조회 수: 4(최근 30일)
Fede C 2018 London 2021년 7월 2일
댓글: Fede C 2018 London 2021년 7월 4일
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표시숨기기 이전 댓글 수: 2
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

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

### Community Treasure Hunt

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

Start Hunting!