Cumsum Function Reproduction

4 次查看(过去 30 天)
Anthony
Anthony 2011-11-17
I wish to write a function that performs the same thing as the built in cumsum function and I can't figure out how to do it, does anyone have any hints or ideas about how I can write the code? Or how the cumsum function itself is writtten in the Matlab program?

回答(6 个)

Jonathan
Jonathan 2011-11-17
[DELETED]
  4 个评论
Jonathan
Jonathan 2011-11-17
Hmm, you are right. I normally catch that. I didn't consider that this time. Thanks for pointing that out guys.
I am well aware of the reasons to not post solutions to homework, but thanks for the lecture anyway. c-;
Jan
Jan 2011-11-17
@Jonathan: Thanks for hidign the solution.
I've mentioned the "copy&paste" term mainly for Anthony.

请先登录,再进行评论。


Jan
Jan 2011-11-17
CUMSUM is written in C and compiled. To create an equivalent function in Matlab you need a FOR loop. At first you start with the first element (or vector, if you want to operate on matrices). Then you add this cummulatively to the following elements / vectors.
I assume, this is a homework. You need three lines only, therefore it is hard to give further advices.

Anthony
Anthony 2011-11-17
For the person concerned with copy and pasting, I was asking for assistancem not answers, thank you for questioning my academic integrity, thank you for the help.
  2 个评论
Jan
Jan 2011-11-17
You have asked for "hints or ideas". Therefore your question welcome in this forum.
Only for the one, who want to answer, it is not trivial to estimate, how much help is still helpful for homework questions.
Walter Roberson
Walter Roberson 2011-11-17
Anthony, the principles of the solution are so basic that once you had seen the solution, you would have had to go through a lot of trouble to deliberately invent a "perverse" solution just to avoid accusations of copying. You would have had to shown Jonathan's solution in your documentation, and then spent a bunch of time in your documentation proving that your own solution wasn't "really" copying his, which would have been a very difficult argument to make, as there just aren't many ways to program this functionality.

请先登录,再进行评论。


Anthony
Anthony 2011-11-17
Once again, wasn't asking for the solution. I'm trying to figure it out and it's not working. There aren't many places or people for me to turn to for assistance so I figured this would be a good place to start.
  3 个评论
Jan
Jan 2011-11-17
Everything is fine, Anthony. You show a lot of interested and without doubt yu are not a lazy cheater.
Let's come back to Matlab. Does my hint help, that you need a FOR loop? You can post, what you have tried so far and ask specific questions.
Walter Roberson
Walter Roberson 2011-11-17
Right: Jan is indicating that you are now better protected because you can no longer be thought to have copy and pasted your solution.
Jan also posted an outline of what has to be done. There is not much more that can be added to what he said without effectively showing the code.
If you were to post what you have already, then people could look and perhaps indicate the concept that you missed.

请先登录,再进行评论。


Anthony
Anthony 2011-11-17
Okay well I've tried a couple different methods but here is what I've been working on now.
function a = mycumsum(x)
for i = 2:length(x)
a = zeros(1,length(x));
a(1) = x(1);
a(i) = x(i)+ x(i-1);
end
Output: a=[1 2 3 4]; mycumsum(a)= [1 0 0 7]
  8 个评论
Jan
Jan 2011-11-18
@Anthony: Fine, the problem is solved partially!
Let's look, what happens inside the loop: In each iteration you create a new vector "a" overwriting the formerly calculated values. This is not wanted.
What about re-using "x" itself?
Jonathan
Jonathan 2011-11-18
It looks like it would be good to write out what happens in each iteration of the for loop. Suppose you use the code above, modified by moving the "reset the vector to zero" as suggested below, like this.
function a = mycumsum(x)
a = zeros(1,length(x));
for i = 2:length(x)
a(1) = x(1);
a(i) = x(i)+ x(i-1);
end
For your input a = [1 2 3 4], the execution of mycumsum(a) looks (conceptually) like this inside the function.
x = [1 2 3 4]
a = zeros(1,length(x))
a(1) = x(1)
a(2) = x(2) + x(2-1)
a(1) = x(1)
a(3) = x(3) + x(3-1)
a(1) = x(1)
a(4) = x(4) + x(4-1)
How would you rewrite this (without a loop) to get the answer you want? Then we can help you make a loop out of it.

请先登录,再进行评论。


Daniel Shub
Daniel Shub 2011-11-18
I can think of 3 independent ways to implement a basic cumsum (ignoring its ability to handle n-D arrays).
There is a way using a for loop that Jan suggested and Anthony is trying to use.
There is a way using recursion.
There is a way using matrix multiplication (hint tril).
There might also be ways to use accumarray, bsxfun, and arrayfun, although I cannot easily think of how to do it. I am not sure which way is most efficient.
  1 个评论
Jan
Jan 2011-11-18
Most efficient: multi-threaded C-mex with using the Intel Compiler due to the 80 bit storage of the accumulator. Using MSVC leads earlier to the well known rounding effects with [1e17, 1, -1e17].

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by