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

1 visualización (últimos 30 días)
Tatiana Koroliuk
Tatiana Koroliuk el 15 de Mayo de 2020
Respondida: Geoff Hayes el 15 de Mayo de 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?

Respuestas (1)

Geoff Hayes
Geoff Hayes el 15 de Mayo de 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.

Categorías

Más información sobre Matrices and Arrays en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by