Index in Positon 1 is invalid

조회 수: 1 (최근 30일)
Jan Faber
Jan Faber 2022년 4월 29일
답변: Jan Faber 2022년 4월 29일
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