View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
Multidimensional matrix multiplication

4.0 | 2 ratings Rate this file 18 Downloads (last 30 days) File Size: 1.75 KB File ID: #41663 Version: 1.2

Multidimensional matrix multiplication



06 May 2013 (Updated )

The simple function performs fast matrix multiplication within multidimensional arrays.

| Watch this File

File Information

mmat(A,B) performs matrix multiplication, where the 2D matrices are part of multidimensional arrays. It is equivalent to the Matlab built in mtimes function for 2D arrays. However it naturally extends the mtimes function, where the two input arrays can have arbitrary number of extra dimensions. For example:

A = [1 2;2 1];
B = [3 4; 1 2];

mmat(A,B) == mtimes(A,B)

However A and B can be expanded along the 3rd dimension:

A = repmat([1 2; 2 1],[1 1 5]);

C = mmat(A,B) can be also performed, C will contains:
C(:,:,1) = A(:,:,1)*B;
C(:,:,2) = A(:,:,2)*B; ...

In the above example B was expanded along the singleton dimensions to match the size of A for the multiplication.

In the above examples the matrix multiplication was performed along the first two dimensions of A and B. However when called:
then dim selects two dimensions from A and B along which the matrix multiplication should be performed.

For example:
dim = [1 2] - default value
dim = [2 3] - C(ii,:,:) = A(ii,:,:) * B(ii,:,:);

This function contains only simple Matlab script without for loops!
Thus it is clean, fast and no compiling needed!

Please leave a comment if you like/dislike it, or you found a bug!

Required Products MATLAB
MATLAB release MATLAB 8.0 (R2012b)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (8)
30 Nov 2016 Jesse Knight

Can you confirm for which use cases this is faster? Or is it just more flexible and convenient?

Please see this example:

% Want A*B: expected size [500 70 300]
A = randn([500,40]);
B = randn([40,70,300]);
% Using mmat
C = mmat(A,B);
fprintf('mmat time: %.05f seconds\n',toc);
% Using loops
C = zeros([size(A,1),size(B,2),size(B,3)]);
for c = 1:size(B,3)
C(:,:,c) = A*B(:,:,d);
fprintf('loop time: %.05f seconds\n',toc);
% Workstation: MATLAB R2011a, Win-10-64, i7(4GHz), 16GB RAM


mmat time: 2.75476 seconds
loop time: 0.06801 seconds

Not sure why loop is so much faster. Perhaps MATLAB online c-compilation is at work here. Profiler reveals 'repmat' on line 65 and 'bsxfun' on line 68 are taking the most time in mmat in this example.

Comment only
09 Nov 2015 Hildo Bijl

I would recommend defining nD as
nD = max([nDA nDB dim]);
The reason is that if you want to multiply along some high dimension dim, sometimes it may happen to be a singleton dimension. As a result, Matlab lowers the values of ndim(A) and ndim(B), which may cause the script to give an error.

16 Aug 2013 Sandor Toth

Thank you Jorge, indeed when dim not equal to [1 2] line 54/55 can throw an error. I corrected the code!

Comment only
16 Aug 2013 Jorge Campos

A little improvement:

Replace lines 54 and 55 by

nA = [size(A),ones(1,nD-nDA)]; nA = nA(dim);
nB = [size(B),ones(1,nD-nDB)]; nB = nB(dim);

Comment only
22 Jul 2013 Sandor Toth

You are right, the whole intermediate matrix is stored in memory during the calculation to increase speed.


Comment only
22 Jul 2013 Jorge Campos

Excellent and efficient, however it's not applicable to huge arrays

25 Jun 2013 Sandor Toth

You have to add the path of the file to the Matlab search path:

where pathName is the name of the folder where the mmat function is. After that, you can simply call mmat().

Comment only
25 Jun 2013 Shauryam

what path has to be given to the function?

Comment only
07 May 2013 1.1

Corrected a small bug in the code and refreshed the file description.

19 Aug 2013 1.2

Line 54/55 are changed, to fix a bug that appears when dim is not the default [1 2] value.

Contact us