MATLAB: Is there an out of memory error despite memory() stating there is enough

indexingMATLABout of memoryr2011b

I have the following code:
data(ind) = data(ind) + shift;
data(~ind) = data(~ind) - shift;
The first line crashes with an out of memory error.
ind is a large logical vector and data is a large uint8 vector of the same length as ind. They both have the same size, about 90 MB. According to memory(), the Maximum possible array is 301 MB.
I would like to optimize this, but I do not understand how the indexing works. I would guess that a temporary copy of the data array is created, but why does it not fit in the available memory? Perhaps, there are multiple copies, or the copy is not of type uint8?

Best Answer

Mixed arithmetic between integer and double involves converting to double, doing the arithmetic and converting back; see http://uk.mathworks.com/matlabcentral/answers/238372-nan-does-not-operate-symmetrically-with-integer-classes
So just uint8(shift) so that you are doing integer/integer arithmetic and you should be okay.
Related Question