MATLAB Answers


Index exceeds array bounds from CorrCoeff

Asked by cniv_we
on 19 Aug 2019 at 23:11
Latest activity Commented on by cniv_we
on 19 Aug 2019 at 23:40
I am trying to calculate cumulative correlation coefficient matrix from 2 different arrays saved in matrix X
Now I only need to save the correlation coefficient in position (1,2) as corrcoeff typically gives 2x2 elements. However, I got this error code: Index in position 2 exceeds array bounds (must not exceed 1).
What's wrong with code below?
for b = 1:1000
corr_coeff = corrcoef(X(1:b,1),X(1:b,2)) % Calculate the cumulative corr.coeff between X(1:b) of column 1 and X(1:b) of column 2
CC(b,:) = [b corr_coeff(1,2)];
When I do not give the command CC(b,:) = [b corr_coeff(1,2)]; I don't have any problem?


Sign in to comment.


1 Answer

Answer by the cyclist
on 19 Aug 2019 at 23:25
 Accepted Answer

The problem is that in the first iteration of your loop, you are taking the correlation of two scalar values:
which will just give the value 1, not a 2x2 matrix.
Maybe start your loop from b=2?

  1 Comment

on 19 Aug 2019 at 23:40
Ah alright, that works. Thank you.

Sign in to comment.