# Slice, ignoring out of bounds

**URL:** <https://discourse.julialang.org/t/slice-ignoring-out-of-bounds/102286>\
**Category:** General Usage\
**Created:** [July 31, 2023, 4:30am UTC](https://discourse.julialang.org/t/slice-ignoring-out-of-bounds/102286 "2023-07-31T04:30:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [July 31, 2023, 4:30am UTC](https://discourse.julialang.org/t/slice-ignoring-out-of-bounds/102286/1 "2023-07-31T04:30:25Z")

</div>

I want to slice an array ignoring any out-of-bounds indices. Is there a preferred way to do this?

My solution

```julia
safeslice(a, ixs) = a[filter(i -> checkbounds(Bool, a, i), ixs)]
julia> safeslice([10,20,30], -4:2)
2-element Vector{Int64}:
 10
 20

```

---

<div class="post-metadata">

**Author:** ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)\
**Post date:** [July 31, 2023, 4:00pm UTC](https://discourse.julialang.org/t/slice-ignoring-out-of-bounds/102286/2 "2023-07-31T16:00:38Z")

</div>

You should consider something using `intersect` on the `CartesianIndices`

```julia
julia> a = zeros(3,4,5);

julia> a[intersect(CartesianIndices(a),CartesianIndices((3:5,3:5,3:5)))]
1×2×3 Array{Float64, 3}:
[:, :, 1] =
 0.0 0.0

[:, :, 2] =
 0.0 0.0

[:, :, 3] =
 0.0 0.0

```

This will figure out the valid indices up front, so won’t have to check each index individually. In this way, it should be much more efficient (if that matters to your case). It should also make it safe to use with `@inbounds`, which may be necessary to get the full efficiency out of it.

You can also look into the `get` function, if you want some other value returned on the invalid indices (but that does not appear to be your case here).

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [July 31, 2023, 7:17pm UTC](https://discourse.julialang.org/t/slice-ignoring-out-of-bounds/102286/3 "2023-07-31T19:17:18Z")

</div>

```julia
(partialgetindices(a, ixs::T) where T <: AbstractArray{CartesianIndex{S}} where S) =
       a[intersect(CartesianIndices(a), ixs)]

julia> partialgetindices([10,20,30], CartesianIndex.([0,1,2]))
2-element Vector{Int64}:
 10
 20

```

---

<div class="post-metadata">

**Author:** ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)\
**Post date:** [July 31, 2023, 7:43pm UTC](https://discourse.julialang.org/t/slice-ignoring-out-of-bounds/102286/4 "2023-07-31T19:43:37Z")

</div>

Here’s a version I threw together, based on intersecting the `axes` rather than `CartesianIndices` but otherwise very similar. By using `to_indices`, this one supports `:` and `::Int` indices as well. It doesn’t drop `::Int` slices like normal `getindex` does, since it needs to be able to drop if the value is out of range. You might find it slightly more ergonomic.

```julia
julia> partialgetindices(a::AbstractArray{<:Any,N}, ixs::Vararg{Any,N}) where N = a[intersect.(axes(a), to_indices(a, ixs))...]
partialgetindices (generic function with 1 method)

julia> a = zeros(3,4,5);

julia> partialgetindices(a,-10:2,:,1)
2×4×1 Array{Float64, 3}:
[:, :, 1] =
 0.0 0.0 0.0 0.0
 0.0 0.0 0.0 0.0

```
