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

1 visualizzazione (ultimi 30 giorni)
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?

Risposte (1)

Geoff Hayes
Geoff Hayes il 15 Mag 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.

Categorie

Scopri di più su Matrices and Arrays in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by