Accessing one element of an array of Cartesian Indexes

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

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)

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.

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


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

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 .;

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)]

The MWE needs to work on someone’s computer (preferably straight paste into 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.

1 Like

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

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.

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)

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

1 Like

Try this:

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

1 Like

Oh my goodness, that was so simple…Thanks!

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!

1 Like

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

1 Like

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.