# A package for arbitrary array indices?

**URL:** <https://discourse.julialang.org/t/a-package-for-arbitrary-array-indices/107848>\
**Category:** General Usage\
**Tags:** question, package\
**Created:** [December 20, 2023, 11:47am UTC](https://discourse.julialang.org/t/a-package-for-arbitrary-array-indices/107848 "2023-12-20T11:47:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![HenriDeh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrideh/32/8316_2.png) [@HenriDeh](https://discourse.julialang.org/u/HenriDeh)\
**Post date:** [December 20, 2023, 11:47am UTC](https://discourse.julialang.org/t/a-package-for-arbitrary-array-indices/107848/1 "2023-12-20T11:47:55Z")

</div>

Hello,

I am a looking for a package that allows me to dynamically change the indexing of an array. I’m aware of OffsetArrays but this is not what I need.

Say I have an 8 by 8 symmetric matrix A. I have an algorithm that defines the “merging” of two indices. Say that I merge indices 3 and 5 by doing `A[3,:] .= (A[3,:] .+ A[5,:])./2` and the same for the columns. Then I can get rid of 5, in practice I do it with a view and an `InvertedIndex` to exclude 5. So this view is a 7x7 matrix now.

The new elements at index 3 are in fact that of another node from a graph (that was defined by A), node 9. What I would like is a way to change the indexing of A such that  
`A[9,:]` gives me what’s in `A[3,:]`, and `A[3,:]` should be out of bounds. In other words, indexing A maps 3 to 9.

I suppose I could implement that myself but I’d like to avoid the risk of making mistakes if this already exists.

Thanks.

---

<div class="post-metadata">

**Author:** ![RobertGregg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/robertgregg/32/22105_2.png) [@RobertGregg](https://discourse.julialang.org/u/RobertGregg)\
**Post date:** [December 22, 2023, 1:12am UTC](https://discourse.julialang.org/t/a-package-for-arbitrary-array-indices/107848/2 "2023-12-22T01:12:23Z")

</div>

I’m not aware of any packages that allow you to redefine array indices. Maybe a different data structure like a dictionary would be the answer?

```julia
D = Dict(i => A[:,a] for (i,a) in enumerate(axes(A,2)))

```

Which would allow for things like:

```julia
D[9] = (D[3] + D[5])/2
delete!(D,3)
delete!(D,5)

```

There are probably more efficient methods though. If `A` is based on a graph perhaps `MetaGraphs.jl` might be useful?

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [December 22, 2023, 1:22am UTC](https://discourse.julialang.org/t/a-package-for-arbitrary-array-indices/107848/3 "2023-12-22T01:22:25Z")

</div>

Your application seems highly specialized. Maybe you should just build your own array type. Then `getindex` can do whatever you would like.

[https://docs.julialang.org/en/v1/manual/interfaces/#man-interface-array](https://docs.julialang.org/en/v1/manual/interfaces/#man-interface-array)
