Quantcast
Channel: First steps - JuliaLang
Viewing all articles
Browse latest Browse all 2795

When will the original value be changed with the copied value?

$
0
0

@zxjroger wrote:

I find out that the following code will change the value of solution to [1.1, 1.1, 1.1, 1.1], although I don’t really want to change its value. It seems that the value of solution is changing with the value of solution_temp.

solution = ones(4)
step_size = 0.1
for i = 1:length(solution)
    solution_temp = solution
    solution_temp[i] = solution_temp[i] + step_size
end

On the other hand, this will not change the value of solution with the value of solution_temp.

solution_temp = solution
solution_temp = solution_temp .+ step_size

I wonder if there is a general rule to know when the first case will happen and when the second case will happen? Or how should I avoid the first case?

Posts: 9

Participants: 6

Read full topic


Viewing all articles
Browse latest Browse all 2795

Trending Articles