f



Transpose matrix multiply - neat trick!

While browsing the web I came upon this little beauty:

>> A' * x;

may instead be written as

>> (x' * A)';

which avoids computing A' and so saves time & memory! Bargain.

Reference: http://www.imm.dtu.dk/~mj/show.php?content=matlab.php
1
spasmous287 (373)
1/14/2004 4:23:58 AM
comp.soft-sys.matlab 211266 articles. 22 followers. lunamoonmoon (257) is leader. Post Follow

2 Replies
2839 Views

Similar Articles

[PageSpeed] 41

In article <2f066762.0401132023.71e4df9c@posting.google.com>, 
spasmous@yahoo.com says...
> While browsing the web I came upon this little beauty:
> 
> >> A' * x;
> 
> may instead be written as
> 
> >> (x' * A)';
> 
> which avoids computing A' and so saves time & memory! Bargain.
> 
> Reference: http://www.imm.dtu.dk/~mj/show.php?content=matlab.php
> 

Are you using R13 or above?  If you put code in an M-file and have the results 
of A'*b or the other returned in a variable (e.g., c = A'*b), then we have some 
magic inside MATLAB to transform the code into another, more efficient pattern 
that doesn't require the literal transpose, but instead calls the BLAS with an 
appropriate flag.  When we time it here, under the conditions I mention, 
y = A'*b  

takes the same amount of time as

y = (b'*A)'

Soooooo, we encourage you to not change to less obvious code in R13 and above.

--Loren
0
loren5358 (941)
1/14/2004 2:49:01 PM
loren@mathworks.com (Loren Shure) wrote in message news:<MPG.1a6f20e7e5c35ac3989719@news.mathworks.com>...
> In article <2f066762.0401132023.71e4df9c@posting.google.com>, 
> spasmous@yahoo.com says...
> > While browsing the web I came upon this little beauty:
> > 
> > >> A' * x;
> > 
> > may instead be written as
> > 
> > >> (x' * A)';
> > 
> > which avoids computing A' and so saves time & memory! Bargain.
> > 
> > Reference: http://www.imm.dtu.dk/~mj/show.php?content=matlab.php
> > 
> 
> Are you using R13 or above?  If you put code in an M-file and have the results 
> of A'*b or the other returned in a variable (e.g., c = A'*b), then we have some 
> magic inside MATLAB to transform the code into another, more efficient pattern 
> that doesn't require the literal transpose, but instead calls the BLAS with an 
> appropriate flag.  When we time it here, under the conditions I mention, 
> y = A'*b  
> 
> takes the same amount of time as
> 
> y = (b'*A)'
> 
> Soooooo, we encourage you to not change to less obvious code in R13 and above.
> 
> --Loren



Thanks Loren! (I've also replied by email from a different account). I
forgot to say that I only tested it on sparse matrices, where there
does seem to be a benefit. On full matrices, there is no benefit as
you say.
-1
spasmous287 (373)
1/14/2004 8:43:47 PM
Reply: