0

While reading Julia documentation in the section "Noteworthy differences from C/C++":

"Julia arrays are not copied when assigned to another variable. After A = B, changing elements of B will modify A as well. Updating operators like += do not operate in-place, they are equivalent to A = A + B which rebinds the left-hand side to the result of the right-hand side expression."

But I tried this, it is not happening i.e. changing B is not changing A. (I tried with 2x2 Matrix)

Can someone explain what this para means?

i tried the following code:

julia> m=Array(zeros(2,2))
2×2 Matrix{Float64}:
 0.0  0.0
 0.0  0.0

julia> k=m
2×2 Matrix{Float64}:
 0.0  0.0
 0.0  0.0

julia> k
2×2 Matrix{Float64}:
 0.0  0.0
 0.0  0.0

julia> m=Array(ones(2,2))
2×2 Matrix{Float64}:
 1.0  1.0
 1.0  1.0

julia> k
2×2 Matrix{Float64}:
 0.0  0.0
 0.0  0.0
deceze
  • 510,633
  • 85
  • 743
  • 889
Ritu Lahkar
  • 407
  • 1
  • 8
  • 2
    `m=Array(ones(2,2))` does not *modify* the object assigned to `m`, it assigns a completely new object to it. That's not going to reflect on the other object. – deceze Feb 10 '22 at 11:04
  • 1
    Does this answer your question? [Creating copies in Julia with = operator](https://stackoverflow.com/questions/33002572/creating-copies-in-julia-with-operator) Or this: https://stackoverflow.com/q/68193719/1346276? – phipsgabler Feb 10 '22 at 12:27
  • Yeah it answered my question partially. – Ritu Lahkar Feb 10 '22 at 15:07
  • https://sylvaticus.github.io/IntroSPMLJuliaCourse/dev/01_-_JULIA1_-_Basic_Julia_programming/0102-types_and_objects.html#Mutability-property-of-Julia-objects – Antonello Feb 12 '22 at 21:13

1 Answers1

0

As it is explained

changing elements of B will modify A as well

So you can use the dotted version of assignment to change elements:

julia> m .= Array(ones(2,2))

Likan Zhan
  • 1,056
  • 6
  • 14