Beginner's question - why doesn't a variable overwrite when vectorizing a loop?

조회 수: 3 (최근 30일)
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)

채택된 답변

Walter Roberson
Walter Roberson 2014년 2월 5일
  댓글 수: 3
Matt J
Matt J 2014년 2월 6일
편집: Matt J 2014년 2월 6일
Vectorization is all about doing the same thing to every value
@Walter
Actually the way I've seen "vectorization" used, it just means "feeding vector/matrix arguments to optimized MATLAB functions" instead of working on their indiviudal elements manually. Your use of CUMSUM qualifies perfectly as an example of vectorization, even though its not doing the same thing to every value.
B.M.
B.M. 2014년 2월 7일
Ok, I think I get what you're saying, thanks. I'm just going to post the actual question (to the original post) so you can see what the exercise asks.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

B.M.
B.M. 2014년 2월 5일
The above was just an example. I just need to know how to get a variable to overwrite itself outside of a for loop.
  댓글 수: 7
Matt J
Matt J 2014년 2월 7일
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
B.M.
B.M. 2014년 2월 7일
편집: B.M. 2014년 2월 7일
Ah, you're right. Sorry. I've been doing other exercises since I posted this and didn't remember accurately what the issue was here. I'll go back and look again at the earlier examples.
EDIT: I got it. Jeez. I think I was fatigued or something after doing all those exercises, now it seems very clear. Thanks to all of you.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by