Index in position 2 exceeds array bounds (must not exceed 6)?

2 views (last 30 days)
Hi!
I am trying to create a program that reads a cell array and computes the answer ex:
t = {'mult','add','2','3','stop','add','4','5','stop','stop'} (So the equation is (2+3)*(4+5))
ans= 45.0000
here is my current code:
function [prod] = doMult ( expr, pos )
%need to checkt to see if there is another function embedded within this
%one
lm=length(expr)-1
k=pos+1
for i=k:length(expr)-1
o=expr(1,i)
if strcmp(o, 'pow')
expr=doPow(expr, i)
lm=length(expr)-1
elseif strcmp(o,'mult')
expr=doMult(expr, i)
lm=length(expr)-1
elseif strcmp(o, 'div')
expr=doDiv(expr, i)
lm=length(expr)-1
elseif strcmp(o,'add')
expr=doAdd(expr, i)
lm=length(expr)-1
elseif strcmp(o,'sub')
expr=doSub(expr, i)
lm=length(expr)-1
elseif strcmp(o, 'stop')
lm=length(expr)-1
s1='stop'
s2=expr
a=strcmpi(s1,s2) %gives a vector where the value is equal to 1 when it is stop
I=find(a==1) %finds the spot number where it is equal to stop
b=min(I)-1 %gives the end number of the current operation
c=pos+1
d=c+1
ExprNUM=str2double(expr)
for j=d:b
h=ExprNUM(1,j)
v=ExprNUM(1,c)
ExprNUM(1,c)=v*h
end
str = sprintf('%d',ExprNUM(1,c))
%need to return the vector with the value for c replaced and all other
%values used removed so that the function can continue
expr{1,pos}=str %replaces the 'mult' with the final value
w=min(I)
for y=[w:-1:c]
expr(:,y)=[] %removes the rows between final answer and the stop
end
prod=expr
else
end
pos=k-1
end
end
and the other finctions for doAdd and such would contain some variation of this (Basically just editing the equation with variables v and h). I currently keep getting the error 'Index in position 2 exceeds array bounds (must not exceed 6)' which from what I understand means that when expr is recalculated with a length of 6 instead of the original 10, it won't allow an i value larger than 6. so how can I recalculate lm for the new length of expr?
  1 Comment
Geoff Hayes
Geoff Hayes on 17 Feb 2019
Ashley - you may need to show the full error message...presumably it is
o=expr(1,i)
that is causing the error message? What do these functions (the do*) return? the remaining portion of the expression or something else? Since your input to the (say) doMult includes expr and then you overwrite this variable after each call to doAdd, doSub, etc., then is this intentional?

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 17 Feb 2019
for loops always evaluate all of their parameters first and record them before executing the loop body. Therefore the value of length(expr)-1 is recorded as soon as the for loop is reached, and the recorded value is not changed no matter what you do inside the loop. When you then change expr inside the loop, although length(expr) changes, the recorded length - 1 does not change, so you risk indexing outside the changed expr.
If you need the length to be recalculated each iteration, then you need to use while.
I would warn you though that any iteration based upon the absolute position of the operands compared to the length is risky. Each time you change expr, things further on in expr "fall down" to occupy the removed expression, and they can "fall down" into space you have already examined.
Iteration over length(expr) is valid, but only if you leave expr unchanged. Instead of changing expr, create a stack (or series of stacks.) Each operation encountered starts a new local stack. Each number encountered pushes a value onto the active local stack. Each stop encountered activates the current operation on the local stack to calculate a result, and that result goes on the end of the previous local stack.
Question for you: how would you process t = {'2', '3'} ? Is that to be considered an error? How about {'add', '2', '3', 'stop', 'add', '4', '5', 'stop'} -- something that would leave multiple items calculated instead of reducing down to exactly one?

Categories

Find more on Data Type Identification in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!