I am loading a quite large matrix into Matlab. Loading this matrix already pushes Matlab to its limits – but it fits.
Then I do the following and I get an out-of-memory error.
data( :, 2:2:end, :, : ) = - data( :, 2:2:end, :, : );
Is Matlab allocating a new matrix for this operation? I would assume this operation would not need extra memory. How do I force Matlab to be more efficient for this?
Bonus question:
‘data = permute(data,[1 2 3 4 5 12 8 7 6 9 10 11]);’
Can matlab do this in-place?
There are a few constraints (further to those from Loren’s block cited by John):
The ‘aliases’ thing is both important and potentially hard to get right. MATLAB uses copy-on-write, which means that when you call a function, the argument you pass isn’t duplicated immediately, but might be copied if you modify it within the function. For example, consider
In that case, the variable
xis passed tomyfcn. MATLAB has value semantics, so any modifications to the input argumentinmust not be seen in the calling workspace. So, the first line ofmyfcncauses the argumentinto become a copy ofx, rather than simply an alias to it. Consider what happens withtry/catch– this can be an in-place killer, because MATLAB has to be able to preserve values if you error out. In the following:then, that should get in-place optimisation for
xinmyouterfcn. But the following can’t:Hope some of this info helps…