what is the fastest way of multiplying a n-way matrix by a vector along a given mode?

I constantly need to multiply a matrix X (dims: n x m x k) with a vector b (dims: k x 1). The end result should be either of (n x m x 1) or preferably (n x m). In addition, is there a function available that would multiply n-way matrix along a given mode by a given vector?
Please note that I would very much like to avoid using any for loops? Currently, I do the job by transforming n-way to a 2-way table and then applying X*b and then transforming it back to (nxmx1) and then squeezing it to (nxm). What I need is a significant speed improvement and I am really hoping that I could find a way to multiply n-way tables directly with a vector.
Thanks in advance.

Answers (2)

The fastest code for small-enough arrays would be to write some assembler code that used the mex interface structure (so it could be called from MATLAB). Be sure to watch out for cache alignment issues. This method would likely be faster than trying to create multiple threads (on small enough vectors) as the overhead of thread communication is not small.
For larger arrays, create a mex interface that marshalls the arguments and calls the BLAS or LAPACK library (I'm not sure which one at the moment.) This will not necessarily end up any faster than MATLAB would generate for a "for" loop. "for" loops are not necessarily slow, especially in R2011b and later.
For even larger arrays, there is a slim chance that using parfor would help. The communications overhead would probably swamp any benefit though.
What does it mean to multiple a n x m x k matrix with a k x 1 matrix? Do you want to scale each n x m submatrix by a scalar. While you do not want loops does
Y = X;
for ii = 1:k
Y(:, :, ii) = X(:, :, ii).*b(ii);
end
do what you want mathematically? If this works, but isn't fast enough, we need the actual sizes of the matrices to attempt to optimize (a large k might lead to a different answer than large n or m).

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 9 May 2012

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!