# Check the sum of a certain array index

**URL:** <https://discourse.julialang.org/t/check-the-sum-of-a-certain-array-index/100552>\
**Category:** General Usage\
**Tags:** indexing, array, sum\
**Created:** [June 19, 2023, 9:44am UTC](https://discourse.julialang.org/t/check-the-sum-of-a-certain-array-index/100552 "2023-06-19T09:44:30Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![divon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/divon/32/50659_2.png) [@divon](https://discourse.julialang.org/u/divon)\
**Post date:** [June 19, 2023, 9:44am UTC](https://discourse.julialang.org/t/check-the-sum-of-a-certain-array-index/100552/1 "2023-06-19T09:44:30Z")

</div>

Hello everyone,

I would like to check the sum of certain indexes in the array. For example, I have the array:

```julia
a = [0, 0, 0, 1, 0, 1, 0, 1]
b = [1, 2, 3, 5]

```

I will check the sum of a[1], a[2], a[3], and a[5]. If the sum is = 0 then a is correct. I am looking forward to any help and replies.

---

<div class="post-metadata">

**Author:** ![SteffenPL](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steffenpl/32/206270_2.png) [@SteffenPL](https://discourse.julialang.org/u/SteffenPL)\
**Post date:** [June 19, 2023, 9:53am UTC](https://discourse.julialang.org/t/check-the-sum-of-a-certain-array-index/100552/2 "2023-06-19T09:53:48Z")

</div>

The easiest way is probably

```julia
sum(a[b])

```

if you want to avoid the extra copy, you could do

```julia
sum(@view a[b])

```

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [June 19, 2023, 10:06am UTC](https://discourse.julialang.org/t/check-the-sum-of-a-certain-array-index/100552/3 "2023-06-19T10:06:59Z")

</div>

Less easy, but benchmarked faster than `@view` method:

```julia
sum(Base.Fix1(getindex,a),b)

```

---

<div class="post-metadata">

**Author:** ![divon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/divon/32/50659_2.png) [@divon](https://discourse.julialang.org/u/divon)\
**Post date:** [June 19, 2023, 10:12am UTC](https://discourse.julialang.org/t/check-the-sum-of-a-certain-array-index/100552/4 "2023-06-19T10:12:49Z")

</div>

Thank you very much for the replies, @SteffenPL and @Dan . I have tried both of your solutions, and they worked very well. Once again, thank you very much.

P.S.:  
I wanted to mark both of the replies as solutions, but I can’t. I am really sorry of it @SteffenPL

---

<div class="post-metadata">

**Author:** ![SteffenPL](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steffenpl/32/206270_2.png) [@SteffenPL](https://discourse.julialang.org/u/SteffenPL)\
**Post date:** [June 19, 2023, 10:17am UTC](https://discourse.julialang.org/t/check-the-sum-of-a-certain-array-index/100552/5 "2023-06-19T10:17:40Z")

</div>

@divon Don’t worry, you should mark the most useful answer as the solution (and it’s not a competition 🙂 )

@Dan Given that @divon is maybe new to Julia, I would maybe recommend

```julia
x = zero(eltype(a)) # or x = 0 (for integers) or x = 1.0 (floats)
for i in b
    x += a[i]
end

```

which is (usually) equally fast and much easier to read (IMO) than the `Base.Fix1` solution.

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [June 19, 2023, 10:42am UTC](https://discourse.julialang.org/t/check-the-sum-of-a-certain-array-index/100552/6 "2023-06-19T10:42:17Z")

</div>

but the “good” vector a could also be like this` [-1,2,-1,99,0,3,3,3]`?

---

<div class="post-metadata">

**Author:** ![divon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/divon/32/50659_2.png) [@divon](https://discourse.julialang.org/u/divon)\
**Post date:** [June 19, 2023, 11:17am UTC](https://discourse.julialang.org/t/check-the-sum-of-a-certain-array-index/100552/7 "2023-06-19T11:17:18Z")

</div>

Thank you very much for your understanding, @SteffenPL. As you said, I am new to Julia. And it is easy to understand your recommendation.

I don’t know about the “good” vector, @rocco_sprmnt21. I wanted to check if the sum of some positions in the array was equal to 0.

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [June 19, 2023, 11:22am UTC](https://discourse.julialang.org/t/check-the-sum-of-a-certain-array-index/100552/8 "2023-06-19T11:22:12Z")

</div>

I used the term “good” because I didn’t remember the one you used: “correct”.  
The meaning of my question is if, given that the example of a=[…] that you proposed has only ‘0’ and ‘1’, the sum must be ‘0’ or all the values must be ‘0’ which is a tighter condition and easier to test, possibly.

---

<div class="post-metadata">

**Author:** ![divon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/divon/32/50659_2.png) [@divon](https://discourse.julialang.org/u/divon)\
**Post date:** [June 19, 2023, 11:36am UTC](https://discourse.julialang.org/t/check-the-sum-of-a-certain-array-index/100552/9 "2023-06-19T11:36:53Z")

</div>

Okay, I get what you mean, @rocco_sprmnt21. Actually, in my simulation, I will have:

```julia
a = [1, 0, 0, 1, 0, 1, 0, 1]
b = [1, 2, 3, 5]
z = [0 0 0 0 0 0 0 0; 0 0 0 0 0 0 0 1; 0 0 0 0 0 0 1 0; 0 0 0 0 0 0 1 1; ...; 1 1 1 1 1 1 1 1]

```

I will do an XOR between a and z, then check that the sum of the results of the XOR is equal to 0. If it is, then I will store which z is the culprit (I hope that you understand).

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [June 19, 2023, 11:40am UTC](https://discourse.julialang.org/t/check-the-sum-of-a-certain-array-index/100552/10 "2023-06-19T11:40:36Z")

</div>

I would try

```julia
sum(a[i] for i in b) 

```

(That’s a response to the original question, not the latest. I don’t understand the latest question, unfortunately.)

---

<div class="post-metadata">

**Author:** ![divon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/divon/32/50659_2.png) [@divon](https://discourse.julialang.org/u/divon)\
**Post date:** [June 19, 2023, 11:47am UTC](https://discourse.julialang.org/t/check-the-sum-of-a-certain-array-index/100552/11 "2023-06-19T11:47:27Z")

</div>

Thank you very much for your reply, @DNF. I have tried it in my simulation, and it returns the same result as other solutions. Also, your solution gives me a new perspective on the for loop.

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [June 19, 2023, 3:12pm UTC](https://discourse.julialang.org/t/check-the-sum-of-a-certain-array-index/100552/12 "2023-06-19T15:12:27Z")

</div>

No, I’m sorry, but I don’t understand what you’re doing and how you transform the data.  
If you give a complete example, maybe it’s easier for me to understand

In any case if a contains only 1 and 0 (or even boolean or even only positive numbers 😁 ) you could also use a different solution than sum

```julia
julia> @btime sum(a[i] for i in b)
  171.777 ns (1 allocation: 16 bytes)
1

julia> @btime all(i->a[i]==0,b)
  120.948 ns (0 allocations: 0 bytes)
false

julia> @btime findfirst(i->a[i]!=0,b) != nothing
  119.912 ns (0 allocations: 0 bytes)
true

```

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [June 19, 2023, 4:30pm UTC](https://discourse.julialang.org/t/check-the-sum-of-a-certain-array-index/100552/13 "2023-06-19T16:30:04Z")

</div>

EDIT: ignore the code below, I forgot that you had to index one array with the values of the other. The best notation is the anonymous function one because you want to a method that takes a function and avoids creating an intermediary array.

> [@rocco\_sprmnt21](#):
>
> ```julia
> julia> @btime all(i->a[i]==0,b)
> 120.948 ns (0 allocations: 0 bytes)
> false
> 
> ```

This can be more succinctly written as `all(iszero, b)`.

> [@rocco\_sprmnt21](#):
>
> ```julia
> julia> @btime findfirst(i->a[i]!=0,b) != nothing
> 119.912 ns (0 allocations: 0 bytes)
> true
> 
> ```

This can be written as `!isnothing(findfirst(!iszero, b))` or `!isnothing(findfirst(>(0), b))` (if only positive numbers are allowed).

You can also replace any `iszero` by `==(0)` or `!iszero` by `!=(0)` but I find those much less legible.

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [June 19, 2023, 7:22pm UTC](https://discourse.julialang.org/t/check-the-sum-of-a-certain-array-index/100552/14 "2023-06-19T19:22:14Z")

</div>

> [@Henrique\_Becker](#):
>
> This can be more succinctly written as `all(iszero, b)`.

b contains the indexes not the values to check.  
In any case it is useful to know the existence of these functions.

---

<div class="post-metadata">

**Author:** ![divon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/divon/32/50659_2.png) [@divon](https://discourse.julialang.org/u/divon)\
**Post date:** [June 20, 2023, 1:20am UTC](https://discourse.julialang.org/t/check-the-sum-of-a-certain-array-index/100552/15 "2023-06-20T01:20:11Z")

</div>

I am sorry for not being detailed, I will try to explain it to you @rocco_sprmnt21.  
In this task, the array is:

```julia
a = [1, 0, 0, 1, 0, 1, 0, 1]

```

Then I need to XOR array a with each of the arrays below:

```julia
z = [0 0 0 0 0 0 0 0; 0 0 0 0 0 0 0 1; 0 0 0 0 0 0 1 0; 0 0 0 0 0 0 1 1; 
     0 0 0 0 0 1 0 0; 0 0 0 0 0 1 0 1; 0 0 0 0 0 1 1 0; 0 0 0 0 0 1 1 1; 
     0 0 0 0 1 0 0 0; 0 0 0 0 1 0 0 1; 0 0 0 0 1 0 1 0; 0 0 0 0 1 0 1 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1; 1 1 1 1 0 1 1 0; 1 1 1 1 0 1 1 1; 
     1 1 1 1 1 0 0 0; 1 1 1 1 1 0 0 1; 1 1 1 1 1 0 1 0; 1 1 1 1 1 0 1 1; 
     1 1 1 1 1 1 0 0; 1 1 1 1 1 1 0 1; 1 1 1 1 1 1 1 0; 1 1 1 1 1 1 1 1; 

```

The XOR operation is using:

```julia
c = zeros(8)
for i in 1 : 2^8
    for j in 1 : 8
        c[j] = xor(a[j], z[i, j]
    end
end

```

Using one of the solutions above, I will check the sum of array c

```julia
res = []
b = [1, 2, 3, 5]
sum_c = sum(Base.Fix1(getindex,a), b)
if sum_c == 0
    res = c
end

```

And use the array that has sum = 0 as the result.

```julia
z = [1, 0, 0, 0, 0, 0, 0, 0]
c = [0, 0, 0, 1, 0, 1, 0, 1]

```

The operation is actually done in the while loop, and when sum\_c = 0, the loop will stop and give the result.  
(I hope you understand now because it is really hard to create array z) 😅

---

<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:** [June 20, 2023, 5:29am UTC](https://discourse.julialang.org/t/check-the-sum-of-a-certain-array-index/100552/16 "2023-06-20T05:29:43Z")

</div>

If this is the actual code, it could be greatly simplified using the following function. You don’t really need to create and store the array `z` before hand. Create the binary representation vector of each byte one by one until your criterion is satisfied. This will be simple and fast because you will return as early as possible.

```julia
function test_xor_sum(a, b)
    for i in 0:255
        z = digits(i, base=2, pad=8)
        all(a[j] == z[j] for j in b) && return z
    end
end

a = [1, 0, 0, 1, 0, 1, 0, 1]
b = [1, 2, 3, 5]
@btime test_xor_sum($a, $b) # 363.462 ns (2 allocations: 256 bytes)

```

---

<div class="post-metadata">

**Author:** ![divon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/divon/32/50659_2.png) [@divon](https://discourse.julialang.org/u/divon)\
**Post date:** [June 20, 2023, 5:52am UTC](https://discourse.julialang.org/t/check-the-sum-of-a-certain-array-index/100552/17 "2023-06-20T05:52:27Z")

</div>

Thank you very much for the solution, @Seif_Shebl. I have tried it and it worked well because, in my previous method (where I needed to generate array z), Julia returned “Out of Memory Error” when I tried to generate an array with length 2^32. 😅

---

<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:** [June 20, 2023, 6:48am UTC](https://discourse.julialang.org/t/check-the-sum-of-a-certain-array-index/100552/18 "2023-06-20T06:48:15Z")

</div>

This could even be made faster if you like `Iterators`. Immutable containers like Tuples allocate zero memory, that’s why it’s faster.

```julia
function test_xor_sum(a, b)
    for z in Iterators.product(ntuple(_->0:1,8)...)
        all(a[j] == z[j] for j in b) && return z
    end
end

a = (1, 0, 0, 1, 0, 1, 0, 1)
b = (1, 2, 3, 5)
@btime test_xor_sum($a, $b) # 140.418 ns (0 allocations: 0 bytes)

```

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [June 20, 2023, 7:33am UTC](https://discourse.julialang.org/t/check-the-sum-of-a-certain-array-index/100552/19 "2023-06-20T07:33:20Z")

</div>

Another way, is to forego all the iterations and consider what the solution should be:

```julia
julia> a = (1, 0, 0, 1, 0, 1, 0, 1)
(1, 0, 0, 1, 0, 1, 0, 1)

julia> b = (1, 2, 3, 5)
(1, 2, 3, 5)

julia> function test_xor_sum2(a, b)
           ntuple(i -> i ∈ b ? a[i] : 0, length(a))
       end
test_xor_sum2 (generic function with 1 method)

julia> test_xor_sum2(a,b)
(1, 0, 0, 0, 0, 0, 0, 0)

julia> @btime test_xor_sum2($a,$b);
  19.748 ns (0 allocations: 0 bytes)

```

This is at least **a solution** , there can be an issue of which solution is returned for which iteration order.

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [June 20, 2023, 7:40am UTC](https://discourse.julialang.org/t/check-the-sum-of-a-certain-array-index/100552/20 "2023-06-20T07:40:34Z")

</div>

I still don’t understand the context.  
However, I add an observation.  
Since we have octets of ‘0’ and ‘1’, the xor operation can be done directly on the decimal numbers corresponding to these octets, rather than vectors (or tuples).  
So there would be no need to construct the z array, for example…

From what I understand so far, it seems more like a problem to be solved by hand.  
A solution could be the following  
The `xor` of `a` with `z` reproduces all `z` in random order (that is, the bit octets of the numbers from ‘0’ to ‘255’).  
Associating `b` to the number 232 = 2^7+2^6+2^5+2^3 a number which certainly has the bits at ‘0’ in the positions (1,2,3,5) from left to rigth is 255-232=23.  
That is 00010111
