Thanks! That works fine.

I must admit, this is all a bit novel for me :) Is my understanding of the 
difference between the following two pieces of code correct:

astore = []
a = [1,2]
b = similar(a)
for j = 1:5


  for i = eachindex(a)
    ai = a[i]
    b[i] = ai + 1
  end
  a = b
  push!(astore,a);
  println(astore)
end

and this code

astore = []
a = [1,2]

for j = 1:5
  b = similar(a)

  for i = eachindex(a)
    ai = a[i]
    b[i] = ai + 1
  end
  a = b
  push!(astore,a);
  println(astore)
end


The first thing we do in the first piece of code is we create a vector b. 
Then we assign (by reference) that a = b, so any changes in b automatically 
flow through to a and, in turn, astore. Thus each loop we change b, and 
thus a, and thus all references to a in astore change.

In the second piece of code, we create a *new* vector b each time. We then 
modify b slightly, and declare a *new* reference a = b. The old values of a 
in astore are now not pointing to the current 'a', but to an older version 
of 'a' (which in turn is another version of 'b' from a previous iteration 
that is still being held in memory.).

Is that correct?

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 <tomm...@live.com.au 
> <javascript:>> 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, ele...@gmail.com 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. 
>

Reply via email to