Consider this excerpt of code:
q = zeros(5,5);
x = rand(5,5);
function testFunc!(n, q, x)
for i = 1:n
q .= x.^2;
end
end
I initialize q and update the values in q through an in-place assignment function. No matter how I choose n, the number of allocations is constant since I'm not creating anything new.
However, is there a way to write a function called testFunc2 that looks like this:
q = zeros(5,5)
x = rand(5,5);
q = testFunc2(n, x)
such that the memory allocation is invariant to n.
I know that this looks silly, but in my actual code, I have numerous variables that are similar to q and I have a loop that updates them each iteration. I don't want the code to occupy excessive memory. If I write a function that looks like testFunc2(n, x, q1, q2, q3, q4), it would look very cumbersome in terms of the syntax. Therefore, I want to be able to have q1, q2, q3, q4 = testFunc2(n, x) without having to worry about memory.