Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to explicitely make a deep copy of an array in MATLAB?

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?

like image 414
f. c. Avatar asked Sep 19 '25 00:09

f. c.


1 Answers

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(:,:);
like image 199
Daniel Avatar answered Sep 20 '25 19:09

Daniel