# PSA: replacement of ind2sub/sub2ind in Julia 0.7+

**URL:** <https://discourse.julialang.org/t/psa-replacement-of-ind2sub-sub2ind-in-julia-0-7/14666>\
**Category:** General Usage\
**Tags:** indexing\
**Created:** [September 7, 2018, 3:19pm UTC](https://discourse.julialang.org/t/psa-replacement-of-ind2sub-sub2ind-in-julia-0-7/14666 "2018-09-07T15:19:07Z")\
**Posts on this page:** 1\
**Showing post:** 30

<div class="post-metadata">

**Author:** ![murphyk](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/murphyk/32/6891_2.png) [@murphyk](https://discourse.julialang.org/u/murphyk)\
**Post date:** [January 27, 2019, 4:05am UTC](https://discourse.julialang.org/t/psa-replacement-of-ind2sub-sub2ind-in-julia-0-7/14666/30 "2019-01-27T04:05:54Z")

</div>

Whoops, first function got cut off. Sorry, trying one more time.

```julia
function lin2cart(shape, indices)
    """
    Transform linear indices to cartesian, cf ind2subv.
    Example:
    lin2cart((3,4,2), [3,5]) = [(3, 1, 1), (2, 2, 1)]
    """
    #https://discourse.julialang.org/t/psa-replacement-of-ind2sub-sub2ind-in-julia-0-7/14666/20
    #return Tuple.(CartesianIndices(shape)[indices])
    return [Base._ind2sub(shape, i) for i in indices]
end

function cart2lin(shape, indices)
    """
    Transform vector of cartesian indices to linear indices, cf subv2ind.
    Example:
    cart2lin([3,4,2], [(3,1,1), (2,2,1)]) = [3, 5]
    """
    #https://discourse.julialang.org/t/psa-replacement-of-ind2sub-sub2ind-in-julia-0-7/14666/20
    #return LinearIndices(shape)[CartesianIndex.(indices)]
    return [Base._sub2ind(shape, i...) for i in indices]
end
```

---

_[View the full topic](https://discourse.julialang.org/t/psa-replacement-of-ind2sub-sub2ind-in-julia-0-7/14666)._
