Cumsum Function Reproduction

9 views (last 30 days)
Anthony
Anthony on 17 Nov 2011
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?

Answers (6)

Jonathan
Jonathan on 17 Nov 2011
[DELETED]
  4 Comments
Jan
Jan on 17 Nov 2011
@Jonathan: Thanks for hidign the solution.
I've mentioned the "copy&paste" term mainly for Anthony.

Sign in to comment.


Jan
Jan on 17 Nov 2011
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 on 17 Nov 2011
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 Comments
Walter Roberson
Walter Roberson on 17 Nov 2011
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.

Sign in to comment.


Anthony
Anthony on 17 Nov 2011
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 Comments
Walter Roberson
Walter Roberson on 17 Nov 2011
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.

Sign in to comment.


Anthony
Anthony on 17 Nov 2011
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 Comments
Jonathan
Jonathan on 18 Nov 2011
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.

Sign in to comment.


Daniel Shub
Daniel Shub on 18 Nov 2011
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 Comment
Jan
Jan on 18 Nov 2011
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].

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!