# How to sum a vector in accumulated form to another vector?

**URL:** <https://discourse.julialang.org/t/how-to-sum-a-vector-in-accumulated-form-to-another-vector/29552>\
**Category:** General Usage\
**Created:** [October 6, 2019, 2:58pm UTC](https://discourse.julialang.org/t/how-to-sum-a-vector-in-accumulated-form-to-another-vector/29552 "2019-10-06T14:58:10Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Halor](https://avatars.discourse-cdn.com/v4/letter/h/4491bb/32.png) [@Halor](https://discourse.julialang.org/u/Halor)\
**Post date:** [October 6, 2019, 2:58pm UTC](https://discourse.julialang.org/t/how-to-sum-a-vector-in-accumulated-form-to-another-vector/29552/1 "2019-10-06T14:58:10Z")

</div>

Hi. I have been struggling trying to figure it out how to sum a vector. For example, I got those numers:

```julia
a= Float64[11]
0.143816064
0.130130156
0.117746635
0.106541561
0.096402791
0.087228852
0.07892793
0.071416944
0.064620723
0.058471248
0.052906973

```

And In another vector I want the sum of the rows in this way:

```julia
row 1 = 0.143816064
row 2 = 0.273946221 (row 1 + row 2).
row 3 = 0.391692855 (row 1 + row 2 + row3)
row 4... = 0.498234416 (row 1 + row 2 + row 3 + row 4...)
... row 11 = 1.008209877 (row 1+... + row 10)

```

Any help would be really appreciated

---

<div class="post-metadata">

**Author:** ![tamasgal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamasgal/32/27946_2.png) [@tamasgal](https://discourse.julialang.org/u/tamasgal)\
**Post date:** [October 6, 2019, 3:07pm UTC](https://discourse.julialang.org/t/how-to-sum-a-vector-in-accumulated-form-to-another-vector/29552/2 "2019-10-06T15:07:08Z")

</div>

```julia
julia> a
10-element Array{Float64,1}:
 0.14606632510509954
 0.1756703572398095
 0.7302286092293604
 0.8201673548813444
 0.47004672453621676
 0.01988181685994972
 0.6152573987998071
 0.36977676750224187
 0.7434864001324983
 0.9583002695417104

julia> cumsum(a)
10-element Array{Float64,1}:
 0.14606632510509954
 0.32173668234490904
 1.0519652915742694
 1.8721326464556138
 2.342179370991831
 2.3620611878517805
 2.9773185866515877
 3.3470953541538293
 4.090581754286328
 5.048882023828038

```

Edit: just noticed there is `cumsum` in base 😉

---

<div class="post-metadata">

**Author:** ![Halor](https://avatars.discourse-cdn.com/v4/letter/h/4491bb/32.png) [@Halor](https://discourse.julialang.org/u/Halor)\
**Post date:** [October 6, 2019, 4:27pm UTC](https://discourse.julialang.org/t/how-to-sum-a-vector-in-accumulated-form-to-another-vector/29552/3 "2019-10-06T16:27:47Z")

</div>

Thank you very much! But the think I want to do is something like this:

For example I want in  
row 1 = 0.143816064.  
row 2 = 0.273946221 (row 1 + row 2).  
row 3 = 0.391692855 (row 1 + row 2 + row3)  
row 4 = 0.498234416 (row 1 + row 2 + row 3 + row 4)

This until row 11, which total sum must be 1.008209877

---

<div class="post-metadata">

**Author:** ![tamasgal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamasgal/32/27946_2.png) [@tamasgal](https://discourse.julialang.org/u/tamasgal)\
**Post date:** [October 6, 2019, 4:29pm UTC](https://discourse.julialang.org/t/how-to-sum-a-vector-in-accumulated-form-to-another-vector/29552/4 "2019-10-06T16:29:12Z")

</div>

Yes, I understood. This is exactly what `cumsum` does 😉

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [October 7, 2019, 1:32am UTC](https://discourse.julialang.org/t/how-to-sum-a-vector-in-accumulated-form-to-another-vector/29552/5 "2019-10-07T01:32:31Z")

</div>

> [@Halor](#):
>
> I have been struggling trying to figure it out how to sum a vector.

While in this case there does happen to be a standard-library function (`cumsum`) that does exactly what you want, if you are “struggling” with this problem then I suspect that you may be approaching Julia in the wrong way.

In some other languages, numerical programming is an exercise in “mining” a set of “standard” libraries, since these are the only fast operations, so even doing something very basic can be a struggle if you don’t know exactly which library function to call.

In Julia, re-using mature library code is still a good thing to do! But it is not your _only_ choice — there is absolutely nothing wrong with writing your own loops. And implementing a trivial function like `cumsum` should not be a struggle unless you are an absolute beginner at programming. For example:

```julia
function my_cumsum(a)
    s = copy(a)
    for i = firstindex(s)+1:lastindex(s)
        s[i] += s[i-1]
    end
    return s
end

```

is a perfectly reasonable implementation. It is a bit slower than `Base.cumsum`, but most of that difference can be made up by adding `@inbounds` to the `s[i] += s[i-1]` line. _If_ this were performance-critical for you (unlikely), the rest of the difference could be made up by avoiding the separate `copy` step (combining the `copy` with the sum):

```julia
function my_cumsum_faster(a)
    s = similar(a)
    isempty(s) && return s
    sum = first(a)
    for i = firstindex(s)+1:lastindex(s)
        @inbounds s[i] = (sum += a[i])
    end
    return s
end

```

This loop is also less accurate than `Base.cumsum`, because the latter uses a [recursive algorithm](https://github.com/JuliaLang/julia/blob/0aa59a0ccdcd47626c2c1be43ebc00f0b36740cb/base/accumulate.jl#L3-L21) called [pairwise summation](https://en.wikipedia.org/wiki/Pairwise_summation) that accumulates roundoff errors much more slowly. Taking advantage of non-obvious algorithms like that written by experienced developers is indeed an advantage of utilizing mature libraries!

However, in a real application you are probably not _just_ doing `cumsum`. Probably you are doing additional operations on the data before or after the summation. In that case, there is often a real advantage in writing your own loop, in order to [avoid multiple passes over the same data or, worse,](https://julialang.org/blog/2017/01/moredots#why-vectorized-code-is-not-as-fast-as-it-could-be) allocating lots of temporary arrays (one for each “vectorized” operation). So, there is a tradeoff, even for using optimized libraries, compared to writing your own code.

Programming is hard for complicated problems! But if you find yourself struggling to code a _simple_ problem, then maybe you are trying too hard to shoehorn your problem into an existing library function rather than just writing your own loop (or recursion, or …). Languages like R, Python, and Matlab train you to “never” write your own inner loops, but in Julia you should unlearn that lesson.

---

<div class="post-metadata">

**Author:** ![Halor](https://avatars.discourse-cdn.com/v4/letter/h/4491bb/32.png) [@Halor](https://discourse.julialang.org/u/Halor)\
**Post date:** [October 7, 2019, 1:58am UTC](https://discourse.julialang.org/t/how-to-sum-a-vector-in-accumulated-form-to-another-vector/29552/6 "2019-10-07T01:58:34Z")

</div>

Thank you very much for your answer!

Actually I am new into programming world. I have not ever program a simple code before. Indeed, Julia is my first approach to. I really appreciated your words and your tips. I must say that it has been very difficult to me.

Anyways, thanks for your help. I was even trying to do a loop but with no success 😂 but with those tips from the user before and yours I have learned alot.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [October 7, 2019, 2:00am UTC](https://discourse.julialang.org/t/how-to-sum-a-vector-in-accumulated-form-to-another-vector/29552/7 "2019-10-07T02:00:48Z")

</div>

> [@Halor](#):
>
> Actually I am new into programming world. I have not ever program a simple code before.

In that case, welcome and enjoy your journey! You will probably want to go through some introductory programming tutorials — for example, [Think Julia](https://benlauwens.github.io/ThinkJulia.jl/latest/book.html) is a new book targeted at absolute beginners, which might be useful for you. And learning to write a function like `cumsum` yourself is a good exercise.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [October 7, 2019, 4:21am UTC](https://discourse.julialang.org/t/how-to-sum-a-vector-in-accumulated-form-to-another-vector/29552/8 "2019-10-07T04:21:13Z")

</div>

no need to feel uncomfortable asking ‘dumb’ question here, this is what community for, after all. Really happy to see people are trying out Julia as their first language.

---

<div class="post-metadata">

**Author:** ![tamasgal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamasgal/32/27946_2.png) [@tamasgal](https://discourse.julialang.org/u/tamasgal)\
**Post date:** [October 7, 2019, 6:57am UTC](https://discourse.julialang.org/t/how-to-sum-a-vector-in-accumulated-form-to-another-vector/29552/9 "2019-10-07T06:57:30Z")

</div>

I wish my first language was Julia 😆

Welcome @Halor and feel free to ask questions 🙂
