index no for iterations

2 views (last 30 days)
Nitheesh M
Nitheesh M on 25 Apr 2015
Answered: Stephen23 on 25 Apr 2015
Attempted to access x(2,1.01); index must be a positive integer or logical.
Error in ==> latest at 19 dx1=x(2,i);
my code ::
for i = 0:0.01:2 yd(j) = sin(2*pi*i); j=j+1; dx1=x(2,i);
should i be integer values only..if then my yd will always be 0 right..i need values of yd from 0 to 2..and corresponding dx1...
please help me out

Accepted Answer

Stephen23
Stephen23 on 25 Apr 2015
You probably don't need a loop at all, and can simply vectorize the code anyway:
x = 0:0.01:2;
y = sin(2*pi*x);
and most likely any following value manipulations can also be vectorized. This is much faster and neater than using loops, which as something low-level programming languages rely on but are not a good use of MATLAB.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!