From: James Tursa on
For some time now I have been under the impression that later versions of MATLAB are smart enough to *not* make a data copy inside a function under certain circumstances when it knows the result is being assigned right back into the original variable. Does anybody know what those circumstances are? e.g., take the following function:

function x = double1(x)
x(1) = 2*x(1);
end

and then the calling sequence:

>> format debug
>> x = rand(1,5)
x =
Structure address = 4811190
m = 1
n = 5
pr = d3804f0
pi = 0
0.8147 0.9058 0.1270 0.9134 0.6324
>> x = double1(x)
x =
Structure address = 4811190
m = 1
n = 5
pr = d380870
pi = 0
1.6294 0.9058 0.1270 0.9134 0.6324
>>

You can see that the pr value has changed, meaning a data copy has occurred. This happens even in R2009b. I would have thought this would be simple enough example for the parser to figure out a data copy was not needed, but it happened anyway. So what are the rules for avoiding the data copy inside a function?

James Tursa
From: Jim Hokanson on
At least for me, if your calling sequence is in a function that operation is in place (in 2009b).

function test
format debug
x = rand(1,5)
x = double1(x)

Structure address = 18999390
m = 1
n = 5
pr = 1e393330
pi = 0
0.7890 0.7949 0.6324 0.8115 0.4481

x =
Structure address = 18999390
m = 1
n = 5
pr = 1e393330
pi = 0
1.5779 1.5898 1.2647 1.6229 0.8962
From: James Tursa on
"Jim Hokanson" <jah104(a)pitt.com> wrote in message <hk0flq$884$1(a)fred.mathworks.com>...
> At least for me, if your calling sequence is in a function that operation is in place (in 2009b).

Thanks. I found Loren's Blog on this subject also, confirming that the call has to be made from inside another function to get the benefit. So apparently no way to do this for variables in the top level workspace other than with a mex routine.

James Tursa