Consider objects a and b, which are array of arrays defined below. These objects are equal according to Julia and the only difference is in how I create them. But double-indexing works very differently on each.
Can anyone explain (1) why a and b behave differently or (2) how I can modify my code to have it always behave like b, which seems like the intuitively correct result?
The issue is not with indexing but at the creation of a : when you use fill([3,4],2), both elements of a point to the same object in memory (here a vector of value [3,4]). You can check that a[1]===a[2] (note the triple = ) whereas b[1] !=== b[2]. Which means that modifying this object will be reflected in all its occurrences.
EDIT : You can completely change a[1] with a[1]=[5,4] (it will not mutate the old a[1] but create a new element that is not linked to “old” a[1] and a[2] (that will be preserved). But I’m not sure it is easy to do otherwise.
My overall goal was to create an array with placeholder NaN values using fill, then use a for loop to replace each entry. This would need a fill function that creates copies instead of references (I’m not sure how to accomplish this but it may involve the deepcopy function).
Some other things that might work: adding one dimension to the array instead of using an array of arrays (this only works if the sizes are consistent), or push!-ing each new array to build up an array over iterations. These are probably better solutions anyway.