Index in Positon 1 is invalid
1 view (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
Voss
on 29 Apr 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
0 Comments
More Answers (2)
Steven Lord
on 29 Apr 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.
0 Comments
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!