# How to sum every element but one

**URL:** <https://discourse.julialang.org/t/how-to-sum-every-element-but-one/49771>\
**Category:** General Usage\
**Tags:** question\
**Created:** [November 8, 2020, 7:18am UTC](https://discourse.julialang.org/t/how-to-sum-every-element-but-one/49771 "2020-11-08T07:18:36Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![carlos](https://avatars.discourse-cdn.com/v4/letter/c/ecae2f/32.png) [@carlos](https://discourse.julialang.org/u/carlos)\
**Post date:** [November 8, 2020, 7:18am UTC](https://discourse.julialang.org/t/how-to-sum-every-element-but-one/49771/1 "2020-11-08T07:18:36Z")

</div>

I want to sum every element of x[i] except when i = j. For example:

> j=10  
> I = 1:30  
> sum(x[i] for i in I) #here I want to sum every element but j.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [November 8, 2020, 7:20am UTC](https://discourse.julialang.org/t/how-to-sum-every-element-but-one/49771/2 "2020-11-08T07:20:49Z")

</div>

There’s always the simple `sum(x)-x[j]`

---

<div class="post-metadata">

**Author:** ![greg\_plowman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greg_plowman/32/8100_2.png) [@greg\_plowman](https://discourse.julialang.org/u/greg_plowman)\
**Post date:** [November 8, 2020, 7:25am UTC](https://discourse.julialang.org/t/how-to-sum-every-element-but-one/49771/3 "2020-11-08T07:25:18Z")

</div>

`sum(x[i] for i in I if i != j)`

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [November 8, 2020, 7:50am UTC](https://discourse.julialang.org/t/how-to-sum-every-element-but-one/49771/4 "2020-11-08T07:50:10Z")

</div>

Unless “the one” element is problematic, e.g., it’s NaN or much larger than the others, summing everything and then subtracting one element is probably the fastest method by far.

If the element is problematic, you could set it to 0 before summing, and then set it back afterwards.

```julia
a = randn(1000)
@btime sum($a) - $a[100] # 154.763 ns (0 allocations: 0 bytes)
@btime sum($a[i] for i in eachindex($a) if i != 100) # 4.415 μs (0 allocations: 0 bytes)

```

The first approach is _much_ faster, but it’s also much more accurate since it uses a highly accurate summing algorithm, which is not used in the second case.

---

<div class="post-metadata">

**Author:** ![carlos](https://avatars.discourse-cdn.com/v4/letter/c/ecae2f/32.png) [@carlos](https://discourse.julialang.org/u/carlos)\
**Post date:** [November 8, 2020, 8:17am UTC](https://discourse.julialang.org/t/how-to-sum-every-element-but-one/49771/5 "2020-11-08T08:17:36Z")

</div>

This is what I needed, thanks.

> [@greg\_plowman](#):
>
> sum(x[i] for i in I if i != j)

---

<div class="post-metadata">

**Author:** ![carlos](https://avatars.discourse-cdn.com/v4/letter/c/ecae2f/32.png) [@carlos](https://discourse.julialang.org/u/carlos)\
**Post date:** [November 8, 2020, 8:18am UTC](https://discourse.julialang.org/t/how-to-sum-every-element-but-one/49771/6 "2020-11-08T08:18:12Z")

</div>

Thanks.

---

<div class="post-metadata">

**Author:** ![carlos](https://avatars.discourse-cdn.com/v4/letter/c/ecae2f/32.png) [@carlos](https://discourse.julialang.org/u/carlos)\
**Post date:** [November 8, 2020, 8:19am UTC](https://discourse.julialang.org/t/how-to-sum-every-element-but-one/49771/7 "2020-11-08T08:19:58Z")

</div>

Thank you for the explanation. I needed the second one.

> [@baggepinnen](#):
>
> `@btime sum($a[i] for i in eachindex($a) if i != 100)`

---

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [November 8, 2020, 11:16am UTC](https://discourse.julialang.org/t/how-to-sum-every-element-but-one/49771/8 "2020-11-08T11:16:59Z")

</div>

Another direct approach, much faster than the generator:

```julia
@btime sum(i -> i == 100 ? 0. : @inbounds($a[i]), eachindex($a))
# 182.628 ns (0 allocations: 0 bytes)

```
