# Cartesian indices, linear indices, and sparse matrices

**URL:** <https://discourse.julialang.org/t/cartesian-indices-linear-indices-and-sparse-matrices/13781>\
**Category:** General Usage\
**Tags:** indexing\
**Created:** [August 20, 2018, 6:56pm UTC](https://discourse.julialang.org/t/cartesian-indices-linear-indices-and-sparse-matrices/13781 "2018-08-20T18:56:22Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [August 20, 2018, 7:59pm UTC](https://discourse.julialang.org/t/cartesian-indices-linear-indices-and-sparse-matrices/13781/2 "2018-08-20T19:59:41Z")

</div>

Thanks for writing this up as a comprehensive question!

1. It’d probably be nice to add a `sparse` method that takes arrays of `CartesianIndex`es. Here’s a simple implementation that could serve you in the mean-time:

2. In the above I used reinterpret and lazy views to prevent needlessly allocating new vectors that contain just those “sub” indices you’re after. But you can use comprehensions to great effect to do this more simply than your `for` loop:

3. To apply a function to transform the cartesian index, I’d just work directly at the level of the cartesian index. E.g.,

---

_[View the full topic](https://discourse.julialang.org/t/cartesian-indices-linear-indices-and-sparse-matrices/13781)._
