When is output assigned from function?
3 次查看(过去 30 天)
显示 更早的评论
I want to understand how functions output the output variable.
For instance, I have the function below.
function A = myfun(X)
len = length(X)
A = zeros(len,1); %preallocate
for ind = 1:len
A = X(1:ind) * X(ind:end) %some operation
end
end
In a script, I call B = myfun(Y)
Does B get updated in the script workspace every time something happens to A? Is there any issue with making multiple assignments to A, or is something like this better:
function A = myfun(X)
len = length(X);
temp_A = zeros(len,1); %preallocate
for ind = 1:len
temp_A = X(1:ind) * X(ind:end); %some operation
end
A = temp_A;
end
Thank you
0 个评论
另请参阅
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!