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

1 Ansicht (letzte 30 Tage)
Tatiana Koroliuk
Tatiana Koroliuk am 15 Mai 2020
Beantwortet: Geoff Hayes am 15 Mai 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?

Antworten (1)

Geoff Hayes
Geoff Hayes am 15 Mai 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.

Kategorien

Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by