Matlab memory optimization regarding output arguments
Mostrar comentarios más antiguos
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.
Respuesta aceptada
Más respuestas (1)
Bruno Luong
el 14 de Dic. de 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
Categorías
Más información sobre Variables en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!