# How to reindex a linear array for cartesian usage?

**URL:** <https://discourse.julialang.org/t/how-to-reindex-a-linear-array-for-cartesian-usage/6723>\
**Category:** General Usage\
**Tags:** question, indexing\
**Created:** [October 27, 2017, 3:16pm UTC](https://discourse.julialang.org/t/how-to-reindex-a-linear-array-for-cartesian-usage/6723 "2017-10-27T15:16:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![standarddeviant](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/standarddeviant/32/2309_2.png) [@standarddeviant](https://discourse.julialang.org/u/standarddeviant)\
**Post date:** [October 27, 2017, 3:16pm UTC](https://discourse.julialang.org/t/how-to-reindex-a-linear-array-for-cartesian-usage/6723/1 "2017-10-27T15:16:52Z")

</div>

Hello,

I’d like to use a Package and the underlying arrays are 2-D. I’m hoping make a new type that uses these 2-D based types, but I’d like the usage of my types to be N-dimensional.

I think I can do this with `sub2ind` and `ind2sub`, but it seems like there might be a better way with `Base.reindex` or possibly `view`.

As an example, if I have  
`A = randn(50,24)`

But I want a new `SubArray` (or something) called B, that represents the 2nd dimension of A to represent 3 dimensions of size (2,3,4). Such that  
`B[2, 2, 2, 2] == A[2, 10]`

The above example corresponds with:

```julia
julia> sub2ind((2,3,4), 2, 2, 2)
10

```

Should I just define `sub2ind` / `ind2sub` on my type or is there a cleaner way to do this?

---

<div class="post-metadata">

**Author:** ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)\
**Post date:** [October 27, 2017, 3:37pm UTC](https://discourse.julialang.org/t/how-to-reindex-a-linear-array-for-cartesian-usage/6723/2 "2017-10-27T15:37:34Z")

</div>

I think

```julia
B = reshape(view(A, :, :), :, 2, 3, 4)

```

is what you want. Note that this returns a `ReshapedArray`, which is a view into `A`, so modifying `B[2, 2, 2, 2]` also modifies `A[2, 10]`.

See also [https://github.com/JuliaLang/julia/issues/24237](https://github.com/JuliaLang/julia/issues/24237).

---

<div class="post-metadata">

**Author:** ![standarddeviant](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/standarddeviant/32/2309_2.png) [@standarddeviant](https://discourse.julialang.org/u/standarddeviant)\
**Post date:** [October 27, 2017, 4:20pm UTC](https://discourse.julialang.org/t/how-to-reindex-a-linear-array-for-cartesian-usage/6723/3 "2017-10-27T16:20:56Z")

</div>

I will give this a try. This sounds promising - thank you!

Update: This definitely works for single elements in the array, although it doesn’t seem to work for slicing/ranges and I’d really like to support slicing with the cartesian coordinates.

Update2: I seem to have mis-spoke.

```julia
julia> A = randn(50,24);

julia> B = reshape(view(A, :, :), :, (2,3,4)...);

julia> getindex(B, 2, 2, 2, 2)
0.06798698740819463

julia> A = randn(50,24);

julia> B = reshape(view(A, :, :), :, (2,3,4)...);

julia> getindex(A, 2, 10)
0.8184511930501794

julia> getindex(B, 2, 2, 2, 2)
0.8184511930501794

julia> getindex(B, 2, 2, 2, 2:4)
3-element Array{Float64,1}:
  0.818451
  0.259312
 -0.196341

julia> B[2,2,2,2:4]
3-element Array{Float64,1}:
  0.818451
  0.259312
 -0.196341

```
