# How to convert multi-dimensional indices into one-dimensional indices

**URL:** <https://discourse.julialang.org/t/how-to-convert-multi-dimensional-indices-into-one-dimensional-indices/65950>\
**Category:** General Usage\
**Tags:** question\
**Created:** [August 6, 2021, 4:23pm UTC](https://discourse.julialang.org/t/how-to-convert-multi-dimensional-indices-into-one-dimensional-indices/65950 "2021-08-06T16:23:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Valery](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/valery/32/21803_2.png) [@Valery](https://discourse.julialang.org/u/Valery)\
**Post date:** [August 6, 2021, 4:23pm UTC](https://discourse.julialang.org/t/how-to-convert-multi-dimensional-indices-into-one-dimensional-indices/65950/1 "2021-08-06T16:23:02Z")

</div>

```julia
A = rand(3,3)
b = findall(A .> .5)

```

For instance:

```julia
3-element Vector{CartesianIndex{2}}:
CartesianIndex(1,1)
CartesianIndex(3,1)
CartesianIndex(2,2)

```

How to do if I want `b` to contain the corresponding `Int` to access the array ? (and not `CartesianIndex`) Is there a way to transform `b` in Int such that the values still correspond to the location in the array `A` ?

For instance, if the `CartesianIndex` is `(1,1)` then the corresponding `Int` is `1` and `A[1]` gives access to the first element of the array whatever its dimensions.

---

<div class="post-metadata">

**Author:** ![jipolanco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jipolanco/32/12129_2.png) [@jipolanco](https://discourse.julialang.org/u/jipolanco)\
**Post date:** [August 6, 2021, 4:28pm UTC](https://discourse.julialang.org/t/how-to-convert-multi-dimensional-indices-into-one-dimensional-indices/65950/2 "2021-08-06T16:28:31Z")

</div>

Here is a simple way of doing that:

```julia
A = rand(3, 3)
I = CartesianIndex(1, 2)
LinearIndices(A)[I] # returns 4

```

---

<div class="post-metadata">

**Author:** ![Valery](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/valery/32/21803_2.png) [@Valery](https://discourse.julialang.org/u/Valery)\
**Post date:** [August 6, 2021, 4:39pm UTC](https://discourse.julialang.org/t/how-to-convert-multi-dimensional-indices-into-one-dimensional-indices/65950/3 "2021-08-06T16:39:38Z")

</div>

It works !

When writing `A` in `LinearIndices`, does it load the full array ? If yes, is there a way to only give the dimensions of `A` (as we do not need the values in `A`). If it does not load `A`, then it is fine.

---

<div class="post-metadata">

**Author:** ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)\
**Post date:** [August 6, 2021, 5:05pm UTC](https://discourse.julialang.org/t/how-to-convert-multi-dimensional-indices-into-one-dimensional-indices/65950/4 "2021-08-06T17:05:41Z")

</div>

`LinearIndices(A)` is lazy, but `LinearIndices(A)[b]` is not. You can also write `findall(vec(A) .> .5)`, perhaps.
