# Getting original array index from its view with reduced dimensions

**URL:** <https://discourse.julialang.org/t/getting-original-array-index-from-its-view-with-reduced-dimensions/46822>\
**Category:** General Usage\
**Tags:** indexing, arrays, views\
**Created:** [September 18, 2020, 5:03am UTC](https://discourse.julialang.org/t/getting-original-array-index-from-its-view-with-reduced-dimensions/46822 "2020-09-18T05:03:50Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![pauljurczak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pauljurczak/32/921_2.png) [@pauljurczak](https://discourse.julialang.org/u/pauljurczak)\
**Post date:** [September 18, 2020, 5:03am UTC](https://discourse.julialang.org/t/getting-original-array-index-from-its-view-with-reduced-dimensions/46822/1 "2020-09-18T05:03:50Z")

</div>

In this code:

```julia
a = reshape(collect(1:12), 4, 3)
v = view(a, 2, :)

for i in eachindex(v)
  # print corresponding index of array a
end

```

I would like to get corresponding original indices of array `a`, either linear or Cartesian, i.e.:

```julia
  2
  6
 10

```

or

```julia
(2, 1)
(2, 2)
(2, 3)

```

Is it possible?

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [September 18, 2020, 6:12am UTC](https://discourse.julialang.org/t/getting-original-array-index-from-its-view-with-reduced-dimensions/46822/2 "2020-09-18T06:12:57Z")

</div>

Yes!

```julia
julia> parentindices(v)
(2, Base.Slice(Base.OneTo(3)))

```

The 2 is the first dimension (1-d slice) and the 1:3 is the second dimension.  
You can do

```julia
julia> CartesianIndices(parentindices(v))
1×3 CartesianIndices{2,Tuple{UnitRange{Int64},UnitRange{Int64}}}:
 CartesianIndex(2, 1) CartesianIndex(2, 2) CartesianIndex(2, 3)

```

And of course you can convert these to linear indices if you want.

---

<div class="post-metadata">

**Author:** ![pauljurczak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pauljurczak/32/921_2.png) [@pauljurczak](https://discourse.julialang.org/u/pauljurczak)\
**Post date:** [September 18, 2020, 4:44pm UTC](https://discourse.julialang.org/t/getting-original-array-index-from-its-view-with-reduced-dimensions/46822/3 "2020-09-18T16:44:47Z")

</div>

How do I do it in my code example?

```julia
a = reshape(collect(1:12), 4, 3)
v = view(a, 2, :)

for i in eachindex(v)
  println(i, " -> ", #= index of a =#)
end

```

I need both indexes: contiguous linear index of `v` and any type of index of `a`.

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [September 18, 2020, 10:31pm UTC](https://discourse.julialang.org/t/getting-original-array-index-from-its-view-with-reduced-dimensions/46822/4 "2020-09-18T22:31:00Z")

</div>

You can `zip` the two together if you want to stick to the exported api

```julia
for i_view, ci_orig in zip(eachindex(v), CartesianIndices(parentindices(v)))
  # ...
end

```

You can also take a more “under the hood” approach, keeping in mind that implementation details of these types aren’t guaranteed to remain the same (probably still a safe bet in this case)

```julia
for i in eachindex(v)
    i_orig = v.offset1 + v.stride1*i
    #...
end

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [October 30, 2021, 11:08am UTC](https://discourse.julialang.org/t/getting-original-array-index-from-its-view-with-reduced-dimensions/46822/5 "2021-10-30T11:08:42Z")

</div>

@tomerarnon, sorry to revisit this but could not get the expected results.  
Would it be possible to produce a MWE?

By using `Iterators.product` and splatting, the results requested by the OP can be obtained:

```julia
a = reshape(collect(1:12), 4, 3)

v = view(a, 2, :) # view of 1 row
for i_view in Iterators.product(parentindices(v)...)
    println("parent index = $i_view")
end

# result:
parent index = (2, 1)
parent index = (2, 2)
parent index = (2, 3)

```

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [October 30, 2021, 7:09pm UTC](https://discourse.julialang.org/t/getting-original-array-index-from-its-view-with-reduced-dimensions/46822/6 "2021-10-30T19:09:17Z")

</div>

Seems like the behavior of `CartesianIndices` changed at some point.

Julia1.2:

```julia
julia> a = reshape(collect(1:12), 4, 3);

julia> v = view(a, 2, :);

julia> for (i_view, ci_orig) in zip(eachindex(v), CartesianIndices(parentindices(v)))
         @show i_view, ci_orig
       end
(i_view, ci_orig) = (1, CartesianIndex(2, 1))
(i_view, ci_orig) = (2, CartesianIndex(2, 2))
(i_view, ci_orig) = (3, CartesianIndex(2, 3))

```

Julia1.6:

```julia
julia> a = reshape(collect(1:12), 4, 3);

julia> v = view(a, 2, :);

julia> for (i_view, ci_orig) in zip(eachindex(v), CartesianIndices(parentindices(v)))
         @show i_view, ci_orig
       end
(i_view, ci_orig) = (1, CartesianIndex(1, 1))
(i_view, ci_orig) = (2, CartesianIndex(2, 1))
(i_view, ci_orig) = (3, CartesianIndex(1, 2))

```

The second method I gave still works, although ironically it was supposed to be the less stable one…

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [October 30, 2021, 7:17pm UTC](https://discourse.julialang.org/t/getting-original-array-index-from-its-view-with-reduced-dimensions/46822/7 "2021-10-30T19:17:37Z")

</div>

> [@tomerarnon](#):
>
> second method I gave still works

Thanks for the feedback, the second method still works indeed (_the input example is a bit unfortunate because the index numbers are equal to the matrix values_)
