# Accessing one element of an array of Cartesian Indexes

**URL:** <https://discourse.julialang.org/t/accessing-one-element-of-an-array-of-cartesian-indexes/16333>\
**Category:** New to Julia\
**Created:** [October 15, 2018, 4:28am UTC](https://discourse.julialang.org/t/accessing-one-element-of-an-array-of-cartesian-indexes/16333 "2018-10-15T04:28:06Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nakul\_Tiruviluamala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nakul_tiruviluamala/32/11228_2.png) [@Nakul\_Tiruviluamala](https://discourse.julialang.org/u/Nakul_Tiruviluamala)\
**Post date:** [October 15, 2018, 4:28am UTC](https://discourse.julialang.org/t/accessing-one-element-of-an-array-of-cartesian-indexes/16333/1 "2018-10-15T04:28:06Z")

</div>

Hi, I’ve got an array called “Index” of Cartesian Indexes. How do I access a specific element of this array? I’ve tried Index[2] or Index[3] with no avail, it always returns the same thing (printed below).

Thanks,  
Nakul

```julia
Index
10-element Array{CartesianIndex{2},1}:
 CartesianIndex(12, 6) 
 CartesianIndex(24, 6) 
 CartesianIndex(36, 6) 
 CartesianIndex(48, 6) 
 CartesianIndex(60, 6) 
 CartesianIndex(72, 6) 
 CartesianIndex(84, 6) 
 CartesianIndex(96, 6) 
 CartesianIndex(108, 6)
 CartesianIndex(120, 6)

```

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [October 15, 2018, 4:43am UTC](https://discourse.julialang.org/t/accessing-one-element-of-an-array-of-cartesian-indexes/16333/2 "2018-10-15T04:43:50Z")

</div>

Can you post a minimum working example (MWE) with some code that can be run to generate the variable `Index`. Otherwise it’s not clear what you are trying to do or what isn’t working.

---

<div class="post-metadata">

**Author:** ![Nakul\_Tiruviluamala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nakul_tiruviluamala/32/11228_2.png) [@Nakul\_Tiruviluamala](https://discourse.julialang.org/u/Nakul_Tiruviluamala)\
**Post date:** [October 15, 2018, 5:01am UTC](https://discourse.julialang.org/t/accessing-one-element-of-an-array-of-cartesian-indexes/16333/3 "2018-10-15T05:01:28Z")

</div>

MWE below. Essentially what I am trying to do is to do a lookup of my 8cents delimited look-up-table text file for the purposes of converting from one type of musical notation to another.

The alphabetic input “do mi so la ti” should come out looking like a frequency in cents [000 400 700 900 1100], which corresponds to the first element of each line.

findall returns Cartesian indices of the row and column in which the original pitch input exists. I need to dereference just the row of the CartesianIndex because it is the first element of that row which decodes the frequency in cents. I don’t know how to do this.

Thanks,  
Nakul

```julia

using DelimitedFiles
pitchcorpus = readdlm("8cents.txt")
pitchcorpus = string.(pitchcorpus)
Indexes = []
input = ["do" "mi" "so" "la" "ti"]
for i = 1:size(input,2)
    mask = occursin.(input[i],pitchcorpus);
    push!(Indexes,findall(mask))
end

```

Here is the textile 8cents.txt

```julia
000 C . Dbb B# do . ru ty S . sa .;
100 C# Db . . di ra . . R1 . ri .;  
200 D . Ebb Cx re . mu dy R2 G1 ri ga; 
300 D# Eb . . ri me . . R3 G2 ri ga; 
400 E . Fb Dx mi . fu ry . G3 . ga; 
500 F . Gbb E# fa . su my M1 . ma .; 
600 F# Gb . . fi se . . M2 . ma .; 
700 G . Abb Fx so . lu fy P . pa .; 
800 G# Ab . . si le . . D1 . da .; 
900 A . Bbb Gx la . tu sy D2 N1 da ni; 
1000 A# Bb . . li te . . D3 N2 da ni;  
1100 B . Cb Ax ti . du ly . N3 . ni;  
1200 C . Dbb B# do . ru ty S . sa .;  
1300 C# Db . . di ra . . R1 . ri .;
1400 D . Ebb Cx re . mu dy R2 G1 ri ga;
1500 D# Eb . . ri me . . R3 G2 ri ga;
1600 E . Fb Dx mi . fu ry . G3 . ga;
1700 F . Gbb E# fa . su my M1 . ma .;
1800 F# Gb . . fi se . . M2 . ma .;
1900 G . Abb Fx so . lu fy P . pa .;
2000 G# Ab . . si le . . D1 . da .;
2100 A . Bbb Gx la . tu sy D2 N1 da ni;
2200 A# Bb . . li te . . D3 N2 da ni;
2300 B . Cb Ax ti . du ly . N3 . ni;
2400 C . Dbb B# do . ru ty S . sa .;
2500 C# Db . . di ra . . R1 . ri .;
2600 D . Ebb Cx re . mu dy R2 G1 ri ga;
2700 D# Eb . . ri me . . R3 G2 ri ga;
2800 E . Fb Dx mi . fu ry . G3 . ga;
2900 F . Gbb E# fa . su my M1 . ma .;
3000 F# Gb . . fi se . . M2 . ma .;
3100 G . Abb Fx so . lu fy P . pa .;
3200 G# Ab . . si le . . D1 . da .;
3300 A . Bbb Gx la . tu sy D2 N1 da ni;
3400 A# Bb . . li te . . D3 N2 da ni;
3500 B . Cb Ax ti . du ly . N3 . ni;
3600 C . Dbb B# do . ru ty S . sa .;
3700 C# Db . . di ra . . R1 . ri .;
3800 D . Ebb Cx re . mu dy R2 G1 ri ga;
3900 D# Eb . . ri me . . R3 G2 ri ga;
4000 E . Fb Dx mi . fu ry . G3 . ga;
4100 F . Gbb E# fa . su my M1 . ma .;
4200 F# Gb . . fi se . . M2 . ma .;
4300 G . Abb Fx so . lu fy P . pa .;
4400 G# Ab . . si le . . D1 . da .;
4500 A . Bbb Gx la . tu sy D2 N1 da ni;
4600 A# Bb . . li te . . D3 N2 da ni;
4700 B . Cb Ax ti . du ly . N3 . ni;
4800 C . Dbb B# do . ru ty S . sa .;
4900 C# Db . . di ra . . R1 . ri .;
5000 D . Ebb Cx re . mu dy R2 G1 ri ga;
5100 D# Eb . . ri me . . R3 G2 ri ga;
5200 E . Fb Dx mi . fu ry . G3 . ga;
5300 F . Gbb E# fa . su my M1 . ma .;
5400 F# Gb . . fi se . . M2 . ma .;
5500 G . Abb Fx so . lu fy P . pa .;
5600 G# Ab . . si le . . D1 . da .;
5700 A . Bbb Gx la . tu sy D2 N1 da ni;
5800 A# Bb . . li te . . D3 N2 da ni;
5900 B . Cb Ax ti . du ly . N3 . ni;
6000 C . Dbb B# do . ru ty S . sa .; 
6100 C# Db . . di ra . . R1 . ri .; 
6200 D . Ebb Cx re . mu dy R2 G1 ri ga; 
6300 D# Eb . . ri me . . R3 G2 ri ga; 
6400 E . Fb Dx mi . fu ry . G3 . ga; 
6500 F . Gbb E# fa . su my M1 . ma .; 
6600 F# Gb . . fi se . . M2 . ma .; 
6700 G . Abb Fx so . lu fy P . pa .; 
6800 G# Ab . . si le . . D1 . da .; 
6900 A . Bbb Gx la . tu sy D2 N1 da ni; 
7000 A# Bb . . li te . . D3 N2 da ni; 
7100 B . Cb Ax ti . du ly . N3 . ni;
7200 C . Dbb B# do . ru ty S . sa .;
7300 C# Db . . di ra . . R1 . ri .;
7400 D . Ebb Cx re . mu dy R2 G1 ri ga;
7500 D# Eb . . ri me . . R3 G2 ri ga;
7600 E . Fb Dx mi . fu ry . G3 . ga;
7700 F . Gbb E# fa . su my M1 . ma .;
7800 F# Gb . . fi se . . M2 . ma .;
7900 G . Abb Fx so . lu fy P . pa .;
8000 G# Ab . . si le . . D1 . da .;
8100 A . Bbb Gx la . tu sy D2 N1 da ni;
8200 A# Bb . . li te . . D3 N2 da ni;
8300 B . Cb Ax ti . du ly . N3 . ni;
8400 C . Dbb B# do . ru ty S . sa .;
8500 C# Db . . di ra . . R1 . ri .;
8600 D . Ebb Cx re . mu dy R2 G1 ri ga;
8700 D# Eb . . ri me . . R3 G2 ri ga;
8800 E . Fb Dx mi . fu ry . G3 . ga;
8900 F . Gbb E# fa . su my M1 . ma .;
9000 F# Gb . . fi se . . M2 . ma .;
9100 G . Abb Fx so . lu fy P . pa .;
9200 G# Ab . . si le . . D1 . da .;
9300 A . Bbb Gx la . tu sy D2 N1 da ni;
9400 A# Bb . . li te . . D3 N2 da ni;
9500 B . Cb Ax ti . du ly . N3 . ni;
9600 C . Dbb B# do . ru ty S . sa .;
9700 C# Db . . di ra . . R1 . ri .;
9800 D . Ebb Cx re . mu dy R2 G1 ri ga;
9900 D# Eb . . ri me . . R3 G2 ri ga;
10000 E . Fb Dx mi . fu ry . G3 . ga;
10100 F . Gbb E# fa . su my M1 . ma .;
10200 F# Gb . . fi se . . M2 . ma .;
10300 G . Abb Fx so . lu fy P . pa .;
10400 G# Ab . . si le . . D1 . da .;
10500 A . Bbb Gx la . tu sy D2 N1 da ni;
10600 A# Bb . . li te . . D3 N2 da ni;
10700 B . Cb Ax ti . du ly . N3 . ni;
10800 C . Dbb B# do . ru ty S . sa .;
10900 C# Db . . di ra . . R1 . ri .;
11000 D . Ebb Cx re . mu dy R2 G1 ri ga;
11100 D# Eb . . ri me . . R3 G2 ri ga;
11200 E . Fb Dx mi . fu ry . G3 . ga;
11300 F . Gbb E# fa . su my M1 . ma .;
11400 F# Gb . . fi se . . M2 . ma .;
11500 G . Abb Fx so . lu fy P . pa .;
11600 G# Ab . . si le . . D1 . da .;
11700 A . Bbb Gx la . tu sy D2 N1 da ni;
11800 A# Bb . . li te . . D3 N2 da ni;
11900 B . Cb Ax ti . du ly . N3 . ni;
12000 C . Dbb B# do . ru ty S . sa .;
12100 C# Db . . di ra . . R1 . ri .;
12200 D . Ebb Cx re . mu dy R2 G1 ri ga;
12300 D# Eb . . ri me . . R3 G2 ri ga;
12400 E . Fb Dx mi . fu ry . G3 . ga;
12500 F . Gbb E# fa . su my M1 . ma .;
12600 F# Gb . . fi se . . M2 . ma .;
12700 G . Abb Fx so . lu fy P . pa .;

```

```julia

Output: 5-element Array{Any,1}:
 CartesianIndex{2}[CartesianIndex(1, 6), CartesianIndex(13, 6), CartesianIndex(25, 6), CartesianIndex(37, 6), CartesianIndex(49, 6), CartesianIndex(61, 6), CartesianIndex(73, 6), CartesianIndex(85, 6), CartesianIndex(97, 6), CartesianIndex(109, 6), CartesianIndex(121, 6)] 
 CartesianIndex{2}[CartesianIndex(5, 6), CartesianIndex(17, 6), CartesianIndex(29, 6), CartesianIndex(41, 6), CartesianIndex(53, 6), CartesianIndex(65, 6), CartesianIndex(77, 6), CartesianIndex(89, 6), CartesianIndex(101, 6), CartesianIndex(113, 6), CartesianIndex(125, 6)]
 CartesianIndex{2}[CartesianIndex(8, 6), CartesianIndex(20, 6), CartesianIndex(32, 6), CartesianIndex(44, 6), CartesianIndex(56, 6), CartesianIndex(68, 6), CartesianIndex(80, 6), CartesianIndex(92, 6), CartesianIndex(104, 6), CartesianIndex(116, 6), CartesianIndex(128, 6)]
 CartesianIndex{2}[CartesianIndex(10, 6), CartesianIndex(22, 6), CartesianIndex(34, 6), CartesianIndex(46, 6), CartesianIndex(58, 6), CartesianIndex(70, 6), CartesianIndex(82, 6), CartesianIndex(94, 6), CartesianIndex(106, 6), CartesianIndex(118, 6)]                       
 CartesianIndex{2}[CartesianIndex(12, 6), CartesianIndex(24, 6), CartesianIndex(36, 6), CartesianIndex(48, 6), CartesianIndex(60, 6), CartesianIndex(72, 6), CartesianIndex(84, 6), CartesianIndex(96, 6), CartesianIndex(108, 6), CartesianIndex(120, 6)]

```

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [October 15, 2018, 5:09am UTC](https://discourse.julialang.org/t/accessing-one-element-of-an-array-of-cartesian-indexes/16333/4 "2018-10-15T05:09:56Z")

</div>

> [@Nakul\_Tiruviluamala](#):
>
> pitchcorpus = readdlm(“8cents.txt”) pitchcorpus = string.(pitchcorpus)

The MWE needs to work on someone’s computer (preferably straight paste into [Juliabox.com](http://Juliabox.com)) and work. So 8cent.txt obviously isn’t available, so you need to put in the effort to create `pitchcorpus` instead of reading it in. Currently to test your code one needs to download the textfile and run your code. You can save someone who’s trying to help you the part about saving the file. It makes it more likely for someone to help you because the “barrier to help” is reduced if you MWE just works. So next time I would manually code a couple of values of `pitchcorpus` like you have with `input`.

What is your expected output if you say your code doesn’t work? Perhaps rework your MWE as well.

---

<div class="post-metadata">

**Author:** ![Nakul\_Tiruviluamala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nakul_tiruviluamala/32/11228_2.png) [@Nakul\_Tiruviluamala](https://discourse.julialang.org/u/Nakul_Tiruviluamala)\
**Post date:** [October 15, 2018, 5:33am UTC](https://discourse.julialang.org/t/accessing-one-element-of-an-array-of-cartesian-indexes/16333/5 "2018-10-15T05:33:52Z")

</div>

Thanks for the tip. I’ll make sure to do that going forward. I’m just trying JuliaBox for the first time, please bear with me if this is not the greatest notebook: [https://juliabox.com/notebook/notebooks/8cents/8cents.ipynb](https://juliabox.com/notebook/notebooks/8cents/8cents.ipynb)

Essentially, I would like to hack into a specific element of the array of Cartesian indexes. Then I would like to access just the row or column coordinate. I would expect to be able to type Indexes[2,3], and get the third Cartesian Index from the second set of cartesian indexes, but that does not work.

```julia
11-element Array{CartesianIndex{2},1}:
 CartesianIndex(1, 6)  
 CartesianIndex(13, 6) 
 CartesianIndex(25, 6) 
 CartesianIndex(37, 6) 
 CartesianIndex(49, 6) 
 CartesianIndex(61, 6) 
 CartesianIndex(73, 6) 
 CartesianIndex(85, 6) 
 CartesianIndex(97, 6) 
 CartesianIndex(109, 6)
 CartesianIndex(121, 6)

```

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [October 15, 2018, 5:38am UTC](https://discourse.julialang.org/t/accessing-one-element-of-an-array-of-cartesian-indexes/16333/6 "2018-10-15T05:38:25Z")

</div>

> [@Nakul\_Tiruviluamala](#):
>
> Indexes[2,3]

Perhaps you are looking for `Indexed[2][3]` ?

---

<div class="post-metadata">

**Author:** ![Liso](https://avatars.discourse-cdn.com/v4/letter/l/898d66/32.png) [@Liso](https://discourse.julialang.org/u/Liso)\
**Post date:** [October 15, 2018, 5:39am UTC](https://discourse.julialang.org/t/accessing-one-element-of-an-array-of-cartesian-indexes/16333/7 "2018-10-15T05:39:22Z")

</div>

Try this:

```julia
julia> r = CartesianIndex(2,1)
CartesianIndex(2, 1)

julia> r[1] # first element is row (I think :P you could use r[2] if it fits better for you) 
2

julia> a = [1 2 ; 3 4]
2×2 Array{Int64,2}:
 1 2
 3 4

julia> a[r] # value of 2nd row 1st collumn in a == 3
3

julia> a[r[1], :] # r' row from a
2-element Array{Int64,1}:
 3
 4

```

---

<div class="post-metadata">

**Author:** ![Nakul\_Tiruviluamala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nakul_tiruviluamala/32/11228_2.png) [@Nakul\_Tiruviluamala](https://discourse.julialang.org/u/Nakul_Tiruviluamala)\
**Post date:** [October 15, 2018, 5:49am UTC](https://discourse.julialang.org/t/accessing-one-element-of-an-array-of-cartesian-indexes/16333/8 "2018-10-15T05:49:16Z")

</div>

Oh my goodness, that was so simple…Thanks!

---

<div class="post-metadata">

**Author:** ![Nakul\_Tiruviluamala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nakul_tiruviluamala/32/11228_2.png) [@Nakul\_Tiruviluamala](https://discourse.julialang.org/u/Nakul_Tiruviluamala)\
**Post date:** [October 15, 2018, 5:52am UTC](https://discourse.julialang.org/t/accessing-one-element-of-an-array-of-cartesian-indexes/16333/9 "2018-10-15T05:52:39Z")

</div>

This is a great explanation! Thank you. I first learned about CartesianIndex function from your help on my last post and it makes a lot more sense now. Incidentally, Li so are both solfege syllables, a big part of my code. As such, Liso is integral to my code in many ways! Many thanks!

---

<div class="post-metadata">

**Author:** ![bennedich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bennedich/32/4894_2.png) [@bennedich](https://discourse.julialang.org/u/bennedich)\
**Post date:** [October 15, 2018, 10:55am UTC](https://discourse.julialang.org/t/accessing-one-element-of-an-array-of-cartesian-indexes/16333/10 "2018-10-15T10:55:31Z")

</div>

> [@Nakul\_Tiruviluamala](#):
>
> The alphabetic input “do mi so la ti” should come out looking like a frequency in cents [000 400 700 900 1100]

But your pitches match several rows? Why should the input “do” result in “000” and not “1200”? (And if you only ever want the first match, why read cents \>= 1200?)

---

<div class="post-metadata">

**Author:** ![Nakul\_Tiruviluamala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nakul_tiruviluamala/32/11228_2.png) [@Nakul\_Tiruviluamala](https://discourse.julialang.org/u/Nakul_Tiruviluamala)\
**Post date:** [October 16, 2018, 2:15pm UTC](https://discourse.julialang.org/t/accessing-one-element-of-an-array-of-cartesian-indexes/16333/11 "2018-10-16T14:15:26Z")

</div>

I indeed realized this after I had started modifying my code. As such, my actual text file corpus only has 12 entries now. For the sake of recreating the original dilemma I posted the original corpus.
