# How to "localize" slicing an array?

**URL:** <https://discourse.julialang.org/t/how-to-localize-slicing-an-array/71691>\
**Category:** General Usage\
**Tags:** indexing, arrays\
**Created:** [November 17, 2021, 11:24pm UTC](https://discourse.julialang.org/t/how-to-localize-slicing-an-array/71691 "2021-11-17T23:24:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![amrods](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amrods/32/2543_2.png) [@amrods](https://discourse.julialang.org/u/amrods)\
**Post date:** [November 17, 2021, 11:24pm UTC](https://discourse.julialang.org/t/how-to-localize-slicing-an-array/71691/1 "2021-11-17T23:24:45Z")

</div>

How can I “localize” slicing on dimension relative to “available” indices? In the example below, I would need `L[:, j]` to mean “get all elements along dimension 1 relative to elements present in `skipzeros`”. How can I do that?

```julia
L = rand(10, 10)
idx0 = rand(0:1, size(L))
L .= L .* idx0

skipzeros = filter(x -> x ∉ findall(L .== 0), CartesianIndices((1:10, 1:10))

for idx in skipzeros
    i, j = Tuple(idx)
    t1 = L[:, j] .+ 100
end

```

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [November 18, 2021, 7:57am UTC](https://discourse.julialang.org/t/how-to-localize-slicing-an-array/71691/2 "2021-11-18T07:57:39Z")

</div>

Is the following what you want? (taking the liberty to simplify the code a bit)

```julia
L = rand(10, 10) .* rand.(Bool)

skipzeros = findall(!=(0), L)

for idx in skipzeros
    same_2nd = getindex.(skipzeros, 2) .== idx[2]
    t1 = L[skipzeros[same_2nd]] .+ 100
end

```

This makes a vector `skipzeros[same_2nd]` which contains all the Cartesian indices that satisfy the condition. Then instead of slicing with `[:, j]` you can use this list of Cartesian indices to extract the corresponding values from `L`.

---

<div class="post-metadata">

**Author:** ![amrods](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amrods/32/2543_2.png) [@amrods](https://discourse.julialang.org/u/amrods)\
**Post date:** [November 18, 2021, 7:59am UTC](https://discourse.julialang.org/t/how-to-localize-slicing-an-array/71691/3 "2021-11-18T07:59:57Z")

</div>

This does it, thank you. Could there be a more streamlined way?

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [November 18, 2021, 9:15am UTC](https://discourse.julialang.org/t/how-to-localize-slicing-an-array/71691/4 "2021-11-18T09:15:01Z")

</div>

It depends what you really want to do. The given example finds the nonzero values in the same column for each nonzero value in `L`, and adds 100. So if column 3 has 5 nonzero values, it will calculate 5 times the same vector `v .+ 100` (where `v` is the list of nonzero values in column 3). A simpler way to do this is

```julia
L = rand(10, 10) .* rand.(Bool)
cols_nozero = filter.(!iszero, eachcol(L))
[v .+ 100 for v in cols_nozero for _ in v]

```
