# Indexing vectors wtih BitVectors

**URL:** <https://discourse.julialang.org/t/indexing-vectors-wtih-bitvectors/122039>\
**Category:** General Usage\
**Created:** [October 31, 2024, 9:49am UTC](https://discourse.julialang.org/t/indexing-vectors-wtih-bitvectors/122039 "2024-10-31T09:49:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yannik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yannik/32/47957_2.png) [@Yannik](https://discourse.julialang.org/u/Yannik)\
**Post date:** [October 31, 2024, 9:49am UTC](https://discourse.julialang.org/t/indexing-vectors-wtih-bitvectors/122039/1 "2024-10-31T09:49:07Z")

</div>

What is the intended behaivious of using bitvectors to index array?

I have an example similar to

```julia
a = [1,2,3]
indexing = a .< 1 # BitVector: 0, 1, 1
a[indexing][2] = 100

```

but then `a` is still

```julia
> a
[1,2,3]

```

The 100 did not do anything. I would have hoped for

```julia
> a
[1,2,100]

```

---

<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:** [October 31, 2024, 10:54am UTC](https://discourse.julialang.org/t/indexing-vectors-wtih-bitvectors/122039/2 "2024-10-31T10:54:08Z")

</div>

It has nothing to do with BitVector indexing. It’s because slices create copies, so you are modifying a copied subset of `a`. Use `view` instead

```julia
@views a[indexing][2] = 100 

```

or

```julia
view(a, indexing)[2] = 100 

```

---

<div class="post-metadata">

**Author:** ![eldee](https://avatars.discourse-cdn.com/v4/letter/e/b5a626/32.png) [@eldee](https://discourse.julialang.org/u/eldee)\
**Post date:** [October 31, 2024, 10:57am UTC](https://discourse.julialang.org/t/indexing-vectors-wtih-bitvectors/122039/3 "2024-10-31T10:57:04Z")

</div>

(

> [@Yannik](#):
>
> ```julia-auto
> a = [1,2,3]
> indexing = a .< 1 # BitVector: 0, 1, 1
> 
> ```

Note that you probably intended to write ` a .> 1`.  
)

# 

> [@Yannik](#):
>
> The 100 did not do anything.

The problem is that in `a[indexing][2] = 100` you first make a copy via `a[indexing]`. In this copy you then change the second entry to `100`. But this is not reflected in the original `a`.

```julia-repl
julia> b = a[indexing]
2-element Vector{Int64}:
 2
 3

julia> b[2] = 100
100

julia> b
2-element Vector{Int64}:
   2
 100

julia> a
3-element Vector{Int64}:
 1
 2
 3

```

To avoid making a copy, you need to create a `view`:

```julia-repl
julia> @view(a[indexing])[2] = 100 # or view(a, indexing)[2] = 100
100

julia> a
3-element Vector{Int64}:
   1
   2
 100

```

---

<div class="post-metadata">

**Author:** ![Yannik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yannik/32/47957_2.png) [@Yannik](https://discourse.julialang.org/u/Yannik)\
**Post date:** [October 31, 2024, 5:47pm UTC](https://discourse.julialang.org/t/indexing-vectors-wtih-bitvectors/122039/4 "2024-10-31T17:47:34Z")

</div>

thanks

yes it was a typo
