Index Out of Bounds
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
I've inherited a program for vehicle dynamics analysis. The code is about 10 years old and I'm running into all kinds of errors. The code imports raw data into a post processing .m file. At the point where I'm getting an error is where it is defining clock wise and counter-clock wise inputs (ay). The variable "ay" is short for acceleration in the y direction (y direction is perpendicular to the forward motion of the vehicle and a function of time). I've posted the section where I getting this error so I hope it's not too vague. There is about 1600 lines of code that I'm trying to fix and make more robust for our raw data. It might be too much to post that much code; hopefully this works. Thanks for your help!
Here is the error:
??? Attempted to access mm2(3);
index out of bounds because
numel(mm2)=2.
Error in ==> OC_POST at 126
xlow_acc=a2(mm2(sl):mm2(sl+1));
Here is my code:
% Assign CW / CCW data from loops
dum=ones(size(ay));
a1=min(ay,limits*dum); % finds elements below positive maximum
a2=max(a1,-limits*dum); % finds elements above negative maximum
plot(a2)
axis([0 100 -0.2 0.2]);
axis 'auto x'
pause,
i1=1;n=length(a2);j1=1;
while 1
[dum,n1]=max(a2(j1:n));
mm1(i1)=j1-1+n1;i1=i1+1;j1=j1-1+n1;
if min(a2(j1:n))~=-limits, break, end;
[dum,n1]=min(a2(j1:n));
mm1(i1)=j1-1+n1;i1=i1+1;j1=j1-1+n1;
if max(a2(j1:n))~=limits, break, end;
end;
j1=n;
while 1
[dum,n1]=min(a2(j1:-1:1));
mm1(i1)=j1+1-n1;i1=i1+1;j1=j1+1-n1;
if max(a2(j1:-1:1))~=limits, break, end;
[dum,n1]=max(a2(j1:-1:1));
mm1(i1)=j1+1-n1;i1=i1+1;j1=j1+1-n1;
if min(a2(j1:-1:1))~=-limits, break, end;
end;
mm2=sort(mm1);
n=length(mm2);
if a2(mm2(1))==a2(mm2(2)), mm2=mm2(2:n); end;
n=length(mm2);
if a2(mm2(n))==a2(mm2(n-1)), mm2=mm2(1:n-1); end;
n1=ceil(length(mm2)/4);n2=floor(length(mm2)/4);
if a2(mm2(1))>a2(mm2(2)),nl=n1;nh=n2;sl=1;sh=3;
else nl=n2;nh=n1;sl=3;sh=1; end;
xlow_acc=a2(mm2(sl):mm2(sl+1));
ylow_str=swa(mm2(sl):mm2(sl+1));
for i1=2:nl, % original range 2:nl
xlow_acc=[xlow_acc a2(mm2(sl+(i1-1)*4):mm2(sl+1+(i1-1)*4))];
ylow_str=[ylow_str swa(mm2(sl+(i1-1)*4):mm2(sl+1+(i1-1)*4))];
end;
xhigh_acc=a2(mm2(sh):mm2(sh+1));
yhigh_str=swa(mm2(sh):mm2(sh+1));
for i1=2:nh, % original range 2:nh
xhigh_acc=[xhigh_acc a2(mm2(sh+(i1-1)*4):mm2(sh+1+(i1-1)*4))];
yhigh_str=[yhigh_str swa(mm2(sh+(i1-1)*4):mm2(sh+1+(i1-1)*4))];
end;
3 comentarios
Respuesta aceptada
Walter Roberson
el 24 de Mayo de 2011
The code can end up with arbitrarily small mm1 or mm2, if the accelerations do not reach the limits.
It also appears to me that nearly all the a2 references are programmed incorrectly. There is no good reason to deliberately index them backwards (j1:-1:1), with the marginal exception that in min() or max() calls it makes a difference as to which copy of the minimum has its index returned, which would lead to mm1 and mm2 being constructed with the values in a different order. mm1 and mm2 are, however, sorted afterwards, so a difference in the order of construction will not make any difference in the result.
When I say that the references are "programmed incorrectly" I mean in the sense that it is incorrect programming practice to obscure the meaning of a section of code without good reason: it leads to errors and difficulty in debugging and greatly magnifies maintenance costs.
Más respuestas (1)
bym
el 24 de Mayo de 2011
apparently the code does not account for length of mm2 being 2. Other than that I can't decipher it.
2 comentarios
Walter Roberson
el 24 de Mayo de 2011
mm2 is created dynamically, starting with a single element. New elements are added each time that it is found that max(a2) *is* at the limit of acceleration -- because if it is *not* at the limit then you "break" out of the loop and stop dynamically extending mm2.
I presently have no idea why you would want to create one element of mm2 each time you hit the acceleration limit, and I presently have no idea why you would assume that there were at least 3 such places. It is not at all obvious to me what the code is intended to do.
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!