Matlab memory optimization regarding output arguments
Show older comments
Suppose we have the following code in functie.m file:
"
function functie()
x = [9];
x = functieLocala(x);
disp(x);
end
function y = functieLocala(x)
if length(x) > 10
y = [x 1];
else
y = x;
end
end
"
The question is will Matlab make the output argument of functieLocala y be a copy of the input argument x, or y will share the data of x? On one hand, function called functie tells that x doesn't need to be preserved, but I do not know how functieLocala behaves in terms of copying or sharing the value(s) of the input argument x to the output argument y.
Accepted Answer
More Answers (1)
Bruno Luong
on 14 Dec 2023
You better have your funtion codded like this
function functie()
x = 9; % Remove the braket
x = functieLocala(x);
disp(x);
end
function x = functieLocala(x) % make inplace otput when it can
if size(x,2) > 10 % use size rather than length
x = [x 1];
% remove the else branch
end
end
Categories
Find more on Variables in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!