Info

This question is closed. Reopen it to edit or answer.

why does this error become "Attempted to access sidelobes(2); index out of bounds because numel(sidelobes)=1."

1 view (last 30 days)
i am working with the algorithm and problem. there is a huge lines of code i cant upload it. i just want to know why this error occurs in matlab so i will sort out some solutions for my code? error is
Attempted to access sidelobes(2); index out of bounds because numel(sidelobes)=1.

Answers (1)

Walter Roberson
Walter Roberson on 5 Oct 2015
The error occurs because you tried to access the second element of sidelobes when sidelobes only has one element. There are a lot of different reasons why it might only have one element, including that that is all that was ever put into there. There are a couple of more common reasons the problem might show up:
  • comparisons were done on floating point numbers expecting equality between a computed value and a constant, or expecting equality between two computed values. Due to round-off problems, if you compute two floating point numbers in even trivially different ways the two numbers might come out unequal; floating point numbers should be compared to within a tolerance.
  • an "if" was done on a condition that was being thought of as a scalar (a single value) but a vector or array was involved. When "if" is applied to a vector or array, the condition is only considered to be true if all values in the vector or array are non-zero ("true"). When this is not realized then it is common to end up with the variable undefined or with a variable assigned a scalar when it should be a vector.
  2 Comments
Muhammad Umer
Muhammad Umer on 5 Oct 2015
i have the floating numbers issue in comparison. this is the some portion of code.
maxtem=0;
count=0;
if yax(1)>yax(num1) && yax(1)>yax(2)
count=count+1;
sidelobes(count)=yax(1);
sllphi(count)=phi(1);
end
if yax(num1)>yax(1) && yax(num1)>yax(num1-1)
count=count+1;
sidelobes(count)=yax(num1);
sllphi(count)=phi(num1);
end
for i=2:num1-1
if yax(i)>yax(i+1) && yax(i)>yax(i-1)
count=count+1;
sidelobes(count)=yax(i);
sllphi(count)=phi(i);
end
end
sidelobes=sort(sidelobes,'descend');
upper_bound=180;
lower_bound=180;
y=sidelobes(2)/maxi;
how can i solve this problem?
Walter Roberson
Walter Roberson on 6 Oct 2015
Notice that all the tests are conditional for incrementing count and assigning to sidelobes(count) . It is not obvious that any of the conditions are necessarily true.
You appear to be searching for local peaks. There are other ways of doing that, such as finding the places that diff(yax) transitions between non-negative and negative: non-negative stretches are staying the same or increasing and when you hit negative then you are starting to decline. You could have an indefinite run of staying the same before declining, so you need to decide which one of those identical values is "the" peak.
If your yax values do not have at least two declines in different stretches, then you would not have at least two peaks. You should be examining your yax values to figure out why.
sign(diff(yax))

Community Treasure Hunt

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

Start Hunting!