matlab - Does A Function's Workspace Duplicate A Variable Input? -


i have defined in base workspace variable

a = ones(10);

and create function inputs vector vec1 , gives vec2:

function vec2 = myfun(vec1)  operations vec1  end 

lets make

b = myfun(a); 

in workspace of myfun have variable called vec1 has same values a not in base workspace.

when being in debugging mode , using

dbup; 

i can see 2 different variables a , vec1 in base , myfun workspaces respectively.

is myfun duplicating variable in 2 different workspaces (and therefore using more memory)?

if not case, how work? pointer assigning 2 different names same information?

thank in advance.

matlab uses system commonly called "copy-on-write" avoid making copy of input argument inside function workspace until or unless modify input argument. if not modify input argument, matlab avoid making copy. instance, in code:

function y = functionoflargematrix(x)       y = x(1); 

matlab not make copy of input in workspace of functionoflargematrix, x not being changed in function. if on other hand, called function:

function y = functionoflargematrix2(x)        x(2) = 2;         y = x(1); 

then x being modified inside workspace of functionoflargematrix2, , copy must made.


Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -