# How to find the sum for indices satisfying certain conditions？

**URL:** <https://discourse.julialang.org/t/how-to-find-the-sum-for-indices-satisfying-certain-conditions/77117>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [February 26, 2022, 11:34am UTC](https://discourse.julialang.org/t/how-to-find-the-sum-for-indices-satisfying-certain-conditions/77117 "2022-02-26T11:34:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shouto](https://avatars.discourse-cdn.com/v4/letter/s/b9e5f3/32.png) [@Shouto](https://discourse.julialang.org/u/Shouto)\
**Post date:** [February 26, 2022, 11:34am UTC](https://discourse.julialang.org/t/how-to-find-the-sum-for-indices-satisfying-certain-conditions/77117/1 "2022-02-26T11:34:59Z")

</div>

Hi all, I have the following question.

Assume that we have integer-valued N \>=1 objects such that E\_1,…,E\_N. Each E\_i takes a number between 1 and N, allowing for duplicates.

Let f be a function and X\_1,…,X\_N be N real numbers.

I want to calculate, for i\<j, Σ\_{i,j:Ei≠Ej} f(X\_E\_i)×f(X\_E\_j)

Any ideas?

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [February 26, 2022, 11:53am UTC](https://discourse.julialang.org/t/how-to-find-the-sum-for-indices-satisfying-certain-conditions/77117/2 "2022-02-26T11:53:12Z")

</div>

Hi @Shoutu

I’m unsure if this is homework or if you are a complete beginner…

In any case, here is something to get you started:

```julia
n = 10
e = rand(1:n, n)
@show e

```

should help you with `E` and reading the [documentation](https://docs.julialang.org/en/v1/) you should be able to construct `X`. For the main part you need to fill in the ??? parts of

```julia
f(e, x, k) = ???
@show sum(??? for j in 1:n for i in 1:j-1)

```

then. HTH

---

<div class="post-metadata">

**Author:** ![Shouto](https://avatars.discourse-cdn.com/v4/letter/s/b9e5f3/32.png) [@Shouto](https://discourse.julialang.org/u/Shouto)\
**Post date:** [February 26, 2022, 11:58am UTC](https://discourse.julialang.org/t/how-to-find-the-sum-for-indices-satisfying-certain-conditions/77117/3 "2022-02-26T11:58:19Z")

</div>

Thanks a lot goerch!

---

<div class="post-metadata">

**Author:** ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)\
**Post date:** [February 26, 2022, 12:03pm UTC](https://discourse.julialang.org/t/how-to-find-the-sum-for-indices-satisfying-certain-conditions/77117/4 "2022-02-26T12:03:14Z")

</div>

Please show us what you have tried after reading the [documentation](https://docs.julialang.org/en/v1/) as @goerch suggested, and people here can help you if you get stuck at some point.
