From: James Tursa on
"James Tursa" <aclassyguy_with_a_k_not_a_c(a)hotmail.com> wrote in message <i2t2uc$g4b$1(a)fred.mathworks.com>...
> "Matt J " <mattjacREMOVE(a)THISieee.spam> wrote in message <i2sva3$m9$1(a)fred.mathworks.com>...
> > "James Tursa" <aclassyguy_with_a_k_not_a_c(a)hotmail.com> wrote in message <i2sspg$kck$1(a)fred.mathworks.com>...
> >
> > > I would point out that his behavior is a somewhat recent change that came in either R2007b or R2008a. Prior to that, MATLAB would not do the operation in place and would potentially generate an out of memory error for this operation. Apparently the newer versions check the size of the rhs and lhs (and presumably the complexity) to see if an in-place operation will work.
> > =======
> >
> > I think it just checks whether the operation is element-wise. I remember Loren writing something about that.
>
> I did check a C = C + A*B operation with A, B, and C all being matrices and it still put the result in-place in C. I also did various permutations of this, like C = 1.2*C + 3.4*A*B etc and it still put the result in-place. The + operation is of course element-wise and maybe that is the key. What I don't know is if the A*B is done off to the side in new memory and then added into C or if a custom call to a BLAS routine does the whole C=C+A*B operation in-place.

I just checked. I used 6000x6000 matrices for A, B, and C and coded up a simple C-mex routine to call dgemm directly to do the C = C + A*B calculation in-place. Attempting to do C = C + A*B at the MATLAB command line causes an out-of-memory error, whereas the direct call to the BLAS routine dgemm does not. So I think I have my answer. MATLAB does not do the whole calculation in-place. Only the element-wise + operation.

James Tursa
From: Bruno Luong on
Qianqian Fang <fangqq(a)gmail.com> wrote in message <4b984d05-ba1f-4ef7-97ca-fbce6e7c6ea4(a)q35g2000yqn.googlegroups.com>...
> hi
>
> I want to know if matlab support in-place operations for simple
> calculations. This particular question raised when I run a vector
> update with a long index:
>
> vol(longidx)=vol(longidx)+weight*val(goodidx);
>
> Very often, matlab complains that memory is running out. I need
> something like "vol+=newadd" as in C/C++, where the memory is updated
> in-place without creating a temporary variable.
>
> does any one know if matlab is capable of doing this?

You might check out this package:

http://www.mathworks.com/matlabcentral/fileexchange/24576-inplacearray-a-semi-pointer-package-for-matlab

Bruno