Error: index out of bounds

1 view (last 30 days)
pep
pep on 18 Jan 2014
Commented: pep on 18 Jan 2014
Hi I'm trying to execute a program but i keep getting this error:"Attempted to access dp(22); index out of bounds because numel(dp)=21"
I understand what it means, but I'm not able to fixe it, I need some help.
Here is the part of the code with the error:
for k=coef_lms:length(x) %length(X)=length(d)=403000, coef_lms=21
xp= x(k-p+1:k);
dp= d(k-p+1:k);
y(k)= h' * xp;
error(k) = dp(k) - y(k);
h = h + mu * xp * conj(error(k));
end
Thanks.

Accepted Answer

Walter Roberson
Walter Roberson on 18 Jan 2014
You have
dp= d(k-p+1:k);
that is going to give you something which is (k)-(k-p+1)+1 = p elements long.
You then have
error(k) = dp(k) - y(k);
which attempts to use dp as if it is k elements long. But dp is p elements long, so that is going to fail unless p >= k. But if p >= k then (k-p+1) of the first line is only going to be positive if k == p exactly. And next iteration, when k increased, you would have to have a failure.
I would point out that you never use any other element of dp within the loop, so there does not seem to be any point in extracting dp exact to be able to find the k'th element of it for the error term. Perhaps what you want is just
dp = d(k);
and then
error(k) = dp - y(k);
I have not examined this for reasonableness of the overall operation, only for consistency in indexing.
  1 Comment
pep
pep on 18 Jan 2014
Thank you very much, I did that and I fixed the error, thanks again.

Sign in to comment.

More Answers (1)

Amit
Amit on 18 Jan 2014
I think you need to rethink how you need to write this. The reason the error is there because in every loop you are defining xp and dp.
Lets say for p is 2, the size of dp and xp in every loop will be only 2 (I think you tried p = 21, here). however in error(k), in every loop you're trying to access the k'th value which doesn't exist.
The moral of the story is:
dp= d(k-p+1:k);
does not creates matrix dp of size 1 to k and fills values only on k-p+1 to K, but it creates a matrix of size 1 to p.
  1 Comment
pep
pep on 18 Jan 2014
Thanks for the explanation, now I could fix the error

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!