0
votes

I have been using Matlab for several years and I am currently learning Julia by translating some m files to jl files.

It's a steep learning curve but going well overall. However, I have struggled with a specific problem for several days now.

Below is a code snippet (does value function iteration) which I use within a function. It works fine in Matlab but - for a reason unknown to me - the variable error_V is 0 after the second iteration of the while loop (so the iteration stops).

I figured out how to use the debugger and my guess is that the command V = TV causes the variable V to get updated dynamically as TV changes inside of the loop.

(I have tried to find out how Julia treats variables from here and here but I am not (yet) able to fully understand the difference between Matlab and Julia in this respect.)

Can someone help or provide further references? Is my guess correct? If yes, how can I change this behavior? If no, what's going on?

Thanks a lot!

TV       = zeros(S,M)
g        = zeros(S,M)
error_V = 1

while error_V > eps_in

  for ii = 1:S
      for jj = 1:M
          U = reshape(U_matrix[ii,jj,:],1,M)
          U_v = U + beta*pi[ii,:]'*V
          TV[ii,jj], g[ii,jj] = findmax(U_v)
      end
  end

  error_V = maximum(maximum(abs.(TV - V)))
  V = TV
end
1
I think I got it... Second item on this list: docs.julialang.org/en/release-0.4/manual/noteworthy-differences Is there any easy workaround? - Atomist

1 Answers

0
votes

As you found out, by default, Julia assignment operator = creates a reference to the variable on the right hand side. If you want to read the value instead you may have to use copy() or deepcopy() to create a shallow or deep copy respectively.

  V = copy(TV)

or

  V = deepcopy(TV)