# Getting linear indices from two vectors containing x and y components of Cartesian coordinates (sub2ind versus LinearIndices())

**URL:** <https://discourse.julialang.org/t/getting-linear-indices-from-two-vectors-containing-x-and-y-components-of-cartesian-coordinates-sub2ind-versus-linearindices/51620>\
**Category:** General Usage\
**Tags:** question, indexing, matrices\
**Created:** [December 10, 2020, 5:30pm UTC](https://discourse.julialang.org/t/getting-linear-indices-from-two-vectors-containing-x-and-y-components-of-cartesian-coordinates-sub2ind-versus-linearindices/51620 "2020-12-10T17:30:07Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)\
**Post date:** [December 10, 2020, 9:52pm UTC](https://discourse.julialang.org/t/getting-linear-indices-from-two-vectors-containing-x-and-y-components-of-cartesian-coordinates-sub2ind-versus-linearindices/51620/2 "2020-12-10T21:52:22Z")

</div>

A long discussion about this:

> [@PSA: replacement of ind2sub/sub2ind in Julia 0.7+](https://discourse.julialang.org/t/psa-replacement-of-ind2sub-sub2ind-in-julia-0-7/14666):
>
> I’ve now seen a couple of cases where folks seem unhappy about the removal of the functions ind2sub and sub2ind. Some of the concerns may stem from the suggested replacement in the deprecation warning, as the replacement, while “safe,” seems considerably more awkward. This is a short post explaining their replacements and the motivation for the change; once you learn to think in the new way, I’d be surprised if you don’t become a fan of the new approach. In older versions of Julia, the way to c…

where the upshot (as I read it) is: If you really need linear indices, you can get them with:

```julia
ind2subv(shape, indices) = Tuple.(CartesianIndices(shape)[indices])
sub2indv(shape, indices) = LinearIndices(shape)[CartesianIndex.(indices)]

```

One question is, though, why do you need linear indices?

---

_[View the full topic](https://discourse.julialang.org/t/getting-linear-indices-from-two-vectors-containing-x-and-y-components-of-cartesian-coordinates-sub2ind-versus-linearindices/51620)._
