Search code examples
juliavariable-assignmentassignment-operatorcopy-assignment

Is there a better way to copy a matrix in Julia than copy()?


I just realized that the "=" operator in Julia acts more like a pointer for matrices than an assignment. For example, if I have a matrix A and I set a new matrix B with B=A, then any modification to B will also apply to A. Instead, I want to initialize B with A, but any changes that I make to B I do not want to apply to A. My current solution is to use Julia's copy() function, but it seems to take a non-negligible amount of time, and it seems like using copy() is a clunky solution. Any solutions/work-arounds would be greatly appreciated. Thank you!

I have a solution to my problem [copy()], but I assume there is a better solution out there.


Solution

  • similar(A) creates the array without copying the values.