Iter rec - does it always have to be an integer?

I'm just trying to set up some 'for' loops and I have a couple of arrays that don't go up in integer values. I've been using this kind of thing so far: -
iter=1;
for n=1:20;
y=n^2
yrec(iter)=y;
iter=iter+1;
end
How do I make it so the iter value is less than one?

1 comentario

I'm just trying to set up some 'for' loops and I have a couple of arrays that don't go up in integer values. I've been using this kind of thing so far: -
iter=1;
for n=1:20;
y=n^2
yrec(iter)=y;
iter=iter+1;
end
How do I make it so the iter value is less than one?

Iniciar sesión para comentar.

 Respuesta aceptada

Walter Roberson
Walter Roberson el 7 de En. de 2012
You cannot use non-integer indices for arrays. Instead, (for example)
iter=1;
itervals = [];
for n=1:20;
y = n^2
yrec(n) = y;
itervals(n) = iter;
iter = iter + 0.123;
end
Then yrec(K) is the yrec that corresponds to location itervals(K)

4 comentarios

Tom
Tom el 7 de En. de 2012
Hi - thanks for that. Unfortunately I'm too much of a newbie to follow that. Could you give me an example?
Walter Roberson
Walter Roberson el 7 de En. de 2012
That is an example.
Suppose you had to calculate the slope between two points. A common expression for that is (y2-y1)/(x2-x1) . In vector form, (y(2)-y(1))./(x(2)-x(1)) . You probably have little difficulty with this expression: you know that y(1) is the y value that _corresponds_ to x(1) and that y(2) is the y value that _corresponds_ to x(2).
Where you are getting confused in your code is in thinking that x(1) must correspond to (some) function applied to x=1, as in the common mathematical form f(x). MATLAB does not replace that form, it just says that the form is only valid when f is a function (or function handle, or OOP Object), and then when instead f is an array, f(x) indicates indexing, the x'th position in f. x(2) -> the 2nd position in the x array. sin(2) -> sine applied to 2 radians.
Some programming languages use a different notation for subscripts, such as C's x[0] for the first position in x. () for indexing has historically been used in a number of languages. It isn't right or wrong, just a language choice.
Tom
Tom el 9 de En. de 2012
Thanks for your time Walter. I understand your answer now, but I'm still unsure about your example code. If I wanted my intervals for n to be 0.1, how would the code read?
Walter Roberson
Walter Roberson el 9 de En. de 2012
iter=1;
itervals = [];
for n=1:20;
y = n^2
yrec(n) = y;
itervals(n) = iter;
iter = iter + 0.1;
end
The "for" loop controls how many times to execute, and when you choose to have that loop start from 1 and increment by 1 (the default) then the loop variable also gives you the array index you want to use. But the array index is not the same as the value of the point represented -- just like in the slope example, x(1) does not mean x=1 but rather means "the first x". The array itervals that I showed in my example code is recording the actual values for the coordinate.
For example, the array x = [2, 5, 7] is indexed at 1, 2, and 3; x(1) is 2, x(2) is 5, x(3) is 7. If you were using y=x.^2 then the corresponding y would be [4, 25, 49], so you would read off
x(1) is 2 and y(1) corresponds to the function value evaluated at x(1)
x(2) is 5 and y(2) corresponds to the function value evaluated at x(2)

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Productos

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by