Access elements in an array that are not contiguous

i have some array like this x=[2, 1, 1, 0, 1, 0, 1, 0] and i want to show only the first two elements and the last two elements of the array but when i do this x[1:2 & 7:8] it shows me this :

4-element Array{Int64,1}:
 2
 1
 1
 1

which is not true i don’t know why?

julia> 1:2 & 7:8
1:2:7

this is actually 1:(2&7):8 thus 1:2:7
you can’t just do as you feel like and expect Julia to read your mind


julia> x = reverse(collect(1:8))
8-element Array{Int64,1}:
 8
 7
 6
 5
 4
 3
 2
 1

julia> x[[1:2;7:8]]
4-element Array{Int64,1}:
 8
 7
 2
 1

this may be what you wanted

1 Like

yeah that’s exactly the expected output