# Getting the CartesianIndex of specifi array elements

**URL:** https://discourse.julialang.org/t/getting-the-cartesianindex-of-specifi-array-elements/31456
**Category:** General Usage
**Created:** [November 24, 2019, 6:32pm UTC](https://discourse.julialang.org/t/getting-the-cartesianindex-of-specifi-array-elements/31456 "2019-11-24T18:32:39Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Ferran\_Mazzanti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ferran_mazzanti/32/7458_2.png) [@Ferran\_Mazzanti](https://discourse.julialang.org/u/Ferran_Mazzanti)
#### Post date: [November 24, 2019, 6:32pm UTC](https://discourse.julialang.org/t/getting-the-cartesianindex-of-specifi-array-elements/31456/1 "2019-11-24T18:32:39Z")

</div>

Dear all,

I’m wondering if there is a simple way to get the CartesianIndex of speciic array elements. I’m not trying to build the whole list of CartesianIndices of an array, only of some of them.  
I’m asking because in my code I have to generate a big array of a number of dimensions given by the user, and then I need to randomly pick up a few of them (sort of sampling the array), but need to know the indices of that element in the array, as I have to use their values for computing related quantities. That would be something like

```julia
a = randn(10,20,5) # though it could be randn(10,3) or rand(1,2,3,4,5,6,7) or...
idx = rand(1:length(a))

```

iterated many times inside a function etc. The question then is: how do I get the CartesianIndex of a[idx]?

Thanks a lot,

Ferran.

---

<div class="post-metadata">

### Author: ![improbable22](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/improbable22/32/5464_2.png) [@improbable22](https://discourse.julialang.org/u/improbable22)
#### Post date: [November 24, 2019, 11:24pm UTC](https://discourse.julialang.org/t/getting-the-cartesianindex-of-specifi-array-elements/31456/2 "2019-11-24T23:24:34Z")

</div>

You can do `CartesianIndices(a)[idx]` or `rand(CartesianIndices(a))`.
