# Index exceeds the number of array elements (1).

1 view (last 30 days)
shubham shubham on 7 Dec 2019
Commented: Walter Roberson on 9 Dec 2019
for i=1:3
q=s{i,1};
x=sum(q{1});
c=s{i,1};
w=sum(c{2})
if x>=m
x=35;
else
x=(x/m)*100;
x=x*(p/100);
end
z=x+w+max;
t1=z;
disp(t1)
if t1(i) >= b(1)
g=1
elseif t1(i) >= b(2)
g=2
elseif t1(i)>= b(3)
g=3
else
g=4
end % if
f=g;
end
In this code the for loop runs correctly for the first iteration but in the second iteration it shows the error of --Index exceeds the number of array elements (1)

Walter Roberson on 7 Dec 2019
We recommend that you do not use max as a variable name as that interferes with using the function max and confuses readers.
With the information that we have been given so far, we might guess that your variable named max is a global variable that was not initialized.
shubham shubham on 7 Dec 2019
i changed the name of the variable but still the same error
Walter Roberson on 9 Dec 2019
Your x is a scalar; so are your w and maximum, so z=x+w+maxv; is going to give a scalar result. Then t1=z; would be scalar. You then try to access t1(i) but that is a problem when i > 1 because t1 is a scalar.
Perhaps you want t1(i) = z