Index in Positon 1 is invalid

3 visualizaciones (últimos 30 días)
Jan Faber
Jan Faber el 29 de Abr. de 2022
Respondida: Jan Faber el 29 de Abr. de 2022
Hello everyone, sadly I have an error that I personally don't really understand, maybe one of you will able to help me, so thanks a lot in advance for that.
So for a task for the university I'm trying to shorten the script a lot by creating a vector through a loop, but I constantly get the error that my Index is invalid.
My aim is to get a 0:14 long vector that contains the following equation for each row depending on n, but sadly it isn't working as planed, i would be really grateful if someone could help me to find my problem here.
for n=0:14
x1=5*sin(2*pi*F1*(kT-n*T));
x2=0.3*cos(2*pi*F2*(kT-n*T));
x=x1+x2;
xv(n,:)=x
end
Error: Index in position 1 is invalid. Array indices must be positive integers or logical values.
Sincerely Jan C. Faber

Respuesta aceptada

Voss
Voss el 29 de Abr. de 2022
Indexing in MATLAB starts at 1, so when n == 0, referring to xv(n,:) gives you that error.
One way to fix it is to add 1 to n when you use it as an index, so that n = 0 through n = 14 correspond to rows 1 through 15 of xv:
for n=0:14
x1=5*sin(2*pi*F1*(kT-n*T));
x2=0.3*cos(2*pi*F2*(kT-n*T));
x=x1+x2;
% xv(n,:)=x
xv(n+1,:)=x
end

Más respuestas (2)

Steven Lord
Steven Lord el 29 de Abr. de 2022
MATLAB indexing is 1-based not 0-based.
The first element / row / column / etc. of an array in MATLAB is element / row / column / etc. 1 not 0.

Jan Faber
Jan Faber el 29 de Abr. de 2022
If I could, then I would love to accept both answers, thanks a lot both of you.

Categorías

Más información sobre Get Started with MATLAB en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by