# 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:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ajsc4](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ajsc4/32/20167_2.png) [@ajsc4](https://discourse.julialang.org/u/ajsc4)\
**Post date:** [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/1 "2020-12-10T17:30:07Z")

</div>

I have some matlab code that uses the old sub2ind way of getting the linear indices (this is a simplified version that gets at the essence of it):

```julia
row = [1, 2, 3]; # x-component of cartesian index
column = [1, 2, 3]; # y-component of cartesian index
sz = [3 3]; # size of the matrix
ind = sub2ind(sz, row, column);

```

This spits out ind = [1, 5, 9]

Using the LinearIndices() and CartesianIndex() functions that Julia moved to I can implement it like this:

```julia
inds = LinearIndices((3, 3))[CartesianIndex.(row, col)]

```

But this somehow seems clunky. According to Pluto it is also slower that the deprecated sub2ind function:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/1/a/1afd8dc9d63393be897b0e37e7379b871f42826c.png)

Is there a better way to implement this?

---

<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?

---

<div class="post-metadata">

**Author:** ![ajsc4](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ajsc4/32/20167_2.png) [@ajsc4](https://discourse.julialang.org/u/ajsc4)\
**Post date:** [December 11, 2020, 11:37am UTC](https://discourse.julialang.org/t/getting-linear-indices-from-two-vectors-containing-x-and-y-components-of-cartesian-coordinates-sub2ind-versus-linearindices/51620/3 "2020-12-11T11:37:47Z")

</div>

Thanks, I had seen that thread. I see that the implementation is the same as what I ended up with.

I’m using linear indices since in the optical simulation I’m running, the physical pixel locations of lenses are being calculated using cartesian coordinates (essentially creating a square grid of points once combined into a meshgrid).

It seems the most convenient way to then index into the new matrix and set the corresponding pixel coordinates to a certain value is to convert into linear indices. Maybe there’s a more Julianic way to achieve the same result, but it works pretty well as it stands.

---

<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 11, 2020, 2:25pm UTC](https://discourse.julialang.org/t/getting-linear-indices-from-two-vectors-containing-x-and-y-components-of-cartesian-coordinates-sub2ind-versus-linearindices/51620/4 "2020-12-11T14:25:20Z")

</div>

I don’t quite follow your description. The only question I was wondering about was: why not directly iterate over the `CartesianIndices((row, col))`? As in:

```julia
A = rand(4,3);
for idx in CartesianIndices((1:3, 1:2))
  println(A[idx]); 
end

```

But if your solution works, it’s probably not worth worrying about it.
