Index in Positon 1 is invalid

1 次查看(过去 30 天)
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

采纳的回答

Voss
Voss 2022-4-29
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

更多回答(2 个)

Steven Lord
Steven Lord 2022-4-29
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 2022-4-29
If I could, then I would love to accept both answers, thanks a lot both of you.

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by