# Cumsum Function Reproduction

8 views (last 30 days)

Show older comments

##### 0 Comments

### Answers (6)

Jonathan
on 17 Nov 2011

[DELETED]

##### 4 Comments

Jonathan
on 17 Nov 2011

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
on 17 Nov 2011

@Jonathan: Thanks for hidign the solution.

I've mentioned the "copy&paste" term mainly for Anthony.

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.

##### 0 Comments

Anthony
on 17 Nov 2011

##### 2 Comments

Jan
on 17 Nov 2011

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
on 17 Nov 2011

Anthony
on 17 Nov 2011

##### 3 Comments

Jan
on 17 Nov 2011

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
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.

Anthony
on 17 Nov 2011

##### 8 Comments

Jan
on 18 Nov 2011

@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
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.

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
on 18 Nov 2011

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!