MATLAB Answers

Error: Index exceeds the number of array elements(1)

1 view (last 30 days)
Tatiana Koroliuk
Tatiana Koroliuk on 15 May 2020
Answered: Geoff Hayes on 15 May 2020
i=input('Enter і = ');
a=0;
b=0;
z=0;
A = [];
for x=i:(i+8)
cell (size(a)) = (a+(abs((sin(x.*2-1.5))^2+(3*sin(x)))+2.38));
cell (size(b))= b+((cos(x))^2*sin(x.*2-1)+4.29);
z=a*b;
cell (size(a)) = a*b;
A= [A, x, a(x), b(x), z];
end
disp(A)
Error in line 11 A= [A, x, a(x), b(x), z]; How I can solve this problem?

Answers (1)

Geoff Hayes
Geoff Hayes on 15 May 2020
Tatiana - the problem is that a is initialized as a scalar but it is treated as an array at the line
A= [A, x, a(x), b(x), z]; % <---- a(x) is an array
This is true for b as well. So if they should be arrays, then where is the code to update them? Also, the lines
cell (size(a)) = (a+(abs((sin(x.*2-1.5))^2+(3*sin(x)))+2.38));
cell (size(b))= b+((cos(x))^2*sin(x.*2-1)+4.29);
will cause a variable named **cell* to be created. But this is also the name of the built-in MATLAB function cell. I strongly recommend that you rename this variable to something else so that it doesn't cause other bugs with your code.

Community Treasure Hunt

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

Start Hunting!

Translated by