For example, I want to do a deep copy of a
to b
:
>> a=zeros(2,3);
>> b=a;
So here =
creates only a shallow copy. My question is, how to generate a deep copy in this case? I know that I can add a command like
b(1,1)=b(1,1)
to make it a deep copy. But is there a better way to do that?
Matlab does not create a shallow copy, instead it uses copy on write. Except for the runtime, this should be totally transparent to you, meaning matlab creates the copy when required. Still if you want to force a copy, you could use one of the examples mentioned here: https://stackoverflow.com/a/36062575/2732801
B=A(:,:);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With