Indexing vectors wtih BitVectors

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

I have an example similar to

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

but then a is still

> a
[1,2,3]

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

> a
[1,2,100]

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

@views a[indexing][2] = 100 

or

view(a, indexing)[2] = 100 

(

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

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> 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> @view(a[indexing])[2] = 100  # or view(a, indexing)[2] = 100
100

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

thanks

yes it was a typo