From: "Bruno Luong" <b.luong@fogale.findmycountry>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Temporary output when assigning into slices?
Date: Wed, 23 Jan 2013 20:10:08 +0000 (UTC)
Organization: FOGALE nanotech
Lines: 32
Message-ID: <kdpg30$6gl$>
References: <kdpbb4$gtp$>
Reply-To: "Bruno Luong" <b.luong@fogale.findmycountry>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: 1358971808 6677 (23 Jan 2013 20:10:08 GMT)
NNTP-Posting-Date: Wed, 23 Jan 2013 20:10:08 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 390839
Xref: comp.soft-sys.matlab:787235

"Matt J" wrote in message <kdpbb4$gtp$>...
> I've been curious, after various recent observations,

I must miss something, what are discussions?

 about if/when matrix multiplication allocates fresh memory for its output. For example, suppose I do something like
>     A(:,1)=B*x;
> Is this equivalent to
>     z=B*x; %memory allocated here
>     A(:,1)=z;
> Or, does the output of B*x get directly generated in the memory locations occupied by A(:,1)?

On recent MATLAB versions (2010a?), MATLAB is smart and puts directly the output in-place.

>Obviously, the latter would be more efficient, but I wasn't sure how it worked. I know for example that this
>  A(1,:)=B(1,:)*x;
> is equivalent to
>  z=B(1,:); %memory allocated here
>  A(1,:)=z*x;
> so obviously not everything is as well optimized as it could be.

If the resulting is not in a contiguous memory, similar optimization on memory space will slow down the speed. I suspect that is the main reason why the optimization cannot be carried out in the later example.