Path: news.mathworks.com!not-for-mail
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$1@newscl01ah.mathworks.com>
References: <kdpbb4$gtp$1@newscl01ah.mathworks.com>
Reply-To: "Bruno Luong" <b.luong@fogale.findmycountry>
NNTP-Posting-Host: www-00-blr.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: newscl01ah.mathworks.com 1358971808 6677 172.30.248.45 (23 Jan 2013 20:10:08 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Wed, 23 Jan 2013 20:10:08 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 390839
Xref: news.mathworks.com comp.soft-sys.matlab:787235

"Matt J" wrote in message <kdpbb4$gtp$1@newscl01ah.mathworks.com>...
> 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.

Bruno