> > That's right. If you are familiar with pointers then you can think of > all julia variables as pointers to the actual object. You can print > the `id()` of the objects to observe this yourself.
Apparently I meant `object_id()` > >> >> Thanks >> Tom >> >> On Monday, 14 December 2015 11:22:20 UTC+11, Yichao Yu wrote: >>> >>> On Sun, Dec 13, 2015 at 6:59 PM, Thomas Moore <[email protected]> wrote: >>> > Hi, >>> > >>> > Your explanation makes sense, but, coming from MATLAB, I must admit I'm >>> > not >>> > familiar with the numerical values of a particular array (in this case >>> > astore) changing when another variable (in this case a) is changed. I >>> > hope >>> > you don't mind a few follow-up questions :) >>> > >>> > - Is this behaviour explained somewhere in the Julia docs so people can >>> > clearly know which operations create a new array (as in my first >>> > example) >>> > and which operations modify the values of an existing array (as in the >>> > second example)? >>> >>> I don't know if your problem is emphasized enough but the doc for the >>> difference between matlab and julia mentioned that assignments are >>> always by reference[1] an modifying a object (setindex) will show up >>> in all variables referencing it. >>> >>> [1] >>> http://julia.readthedocs.org/en/latest/manual/noteworthy-differences/?highlight=matlab#noteworthy-differences-from-matlab >>> >>> > - Is there a 'Julian' way of getting the result of my first example >>> > while >>> > changing individual elements of 'a' as I've done in the second example? >>> > In >>> > each loop I want to change particular values a[i], and then statically >>> > store >>> > the numerical value of a in astore, before commencing to the next >>> > iteration >>> >>> If all what you do is a simple +1 and the first version is the right >>> way to do it. If you are doing more complicated things on each >>> element, you may consider "devectorize" it in order to avoid allocate >>> a lot of intermediates. >>> >>> e.g. >>> >>> b = similar(a) >>> @inbounds for i in eachindex(a) >>> ai = a[i] >>> b[i] = ai * (ai + 1) + sin(ai) - sqrt(ai) # or whatever you want >>> end >>> a = b >>> >>> > - is there a way to easily do this in Julia? >>> >>> See above. >>> >>> > >>> > Thanks >>> > >>> > On Friday, 11 December 2015 17:07:42 UTC+11, [email protected] wrote: >>> >> >>> >> Looks right to me. >>> >> >>> >> In the first version `a = a + 1` makes `a` refer to a *new* array with >>> >> values one greater and `push!(astore,a)` stores the reference to the >>> >> new >>> >> array in `astore`. >>> >> >>> >> In the second version you modify the values of the array already >>> >> referred >>> >> to by `a` and then push a reference to that array, but every time you >>> >> push >>> >> it is a reference to the same array, so in the end all you have is lots >>> >> of >>> >> references to the same array.
