Beginner's question - why doesn't a variable overwrite when vectorizing a loop?
Mostrar comentarios más antiguos
I am learning using the book "Matlab for Engineers and Scientists." Unfortunately this book does not provide answers to the exercises.
I've never been good at writing for loops, or really understanding how to index well, so I am running into problems.
Here's the one at hand. I need to convert a for loop into a vectorization. For simplicity I've written a short example:
s=0;
for n=1:10;
s=s+n
end
This gives the answers 1, 3, 6, 10, 15, 21, 28, 36, 45, 55. When I try to vectorize it like this, s does not overwrite itself after each iteration, but stays at zero:
s=0;
n=1:10;
s=s+n
This gives s=1:10.
Now if s is a simple variable to explain I could just make it a vector as well, but when I don't know what its values will be I can't specify it beforehand. Any help would be appreciated. I know this is simple, but going through this book, the earlier examples aren't relevant to this type of problem.
EDIT: Here is the actual question:
====================
Work out by hand the output of the following script for n = 4:
n = input( ’Number of terms? ’ );
s = 0;
for k = 1:n
s = s + 1 / (k ˆ 2);
end;
disp(sqrt(6 * s))
If you run this script for larger and larger values of n, you will find that the output approaches a well-known limit. Can you figure out what it is? Now rewrite the script using vectors and array operations. ====================
The limit is pi. When I try to do this with vectors I am entering the following:
====================
s=0;
n=4; (just to start)
k=1:1:n;
s=s+1./k.^2;
a=[sqrt(6.*s)];
====================
Of course, this doesn't work. What am I not getting? (Other that you can't overwrite variables if you aren't doing a for loop...but the exercise asks specifically to do this not using a for loop)
Respuesta aceptada
Más respuestas (1)
B.M.
el 5 de Feb. de 2014
0 votos
7 comentarios
There is no such thing as variables overwriting themselves when you don't use loops. That's a concept you seem to have invented yourself.
In vectorized operations, recursively changing variables, if there are any, are always hidden from you inside black-box MATLAB functions. But even in those functions, a loop is being done to perform the recursive updates. In Walter's answer, for example, the CUMSUM function is internally running a loop very much like the one you posted, except that it is hidden from you in compiled C/C++ code.
Patrik Ek
el 6 de Feb. de 2014
I would say sum(1:10) is fair enough since the output is only a single double
B.M.
el 7 de Feb. de 2014
HINT. You should study the documentation for the SUM command and understand what it does. In particular, notice how it allows you to sum up many numbers without using a loop!
Another piece of advice is to study the output of some of the code you've already written and understand what it has done, e.g.
>> n=4; k=1:n; s=1./k.^2
s =
1.0000 0.2500 0.1111 0.0625
B.M.
el 7 de Feb. de 2014
Matt J
el 7 de Feb. de 2014
I have seen your EDIT and fail to see how it is not a sum. The only difference I see relative to your original example is that the formula for the terms in the series being summed is different 1./k.^2
Categorías
Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!