mtimesm, an efficient nD matrix multiplication routine.
Versión 1.0.6 (3,11 KB) por
wfH
A wrapper of `MTIMESX` and `PAGEMTIMES`. Created for people who have old matlab or have difficulties in compiling mex-file.
MTIMESX (`PAGEMTIMES`) is a very efficient matrix multiply routine for n-Dimensional (nD, n > 2) arrays.
However, you have to update your MATLAB (`PAGEMTIMES` was introduced in R2020b), or build a mex-file.
So, I created this function (m-file) for people who have only old matlab or have difficulties in compiling mex-file.
Run the following codes for demonstration.
ctest = 2000;
nfs1 = 45;
nfs2 = 55;
nks1 = 5;
nks2 = 7;
A = rand(nks1, nks1, nfs1, nfs2);
B = rand(nks2, nks1, nfs1, nfs2);
tic;
for ii = 1:ctest
C1 = zeros(nks1, nks2, nfs1, nfs2);
for ff = 1:nfs1
for gg = 1:nfs2
C1(:,:, ff, gg) = conj(A(:,:, ff, gg)).'*B(:,:, ff, gg).';
end
end
end
t1 = toc
tic, for jj = 1:ctest, C2 = mtimesx(A, 'c', B, 't');end, t2 =toc
tic; for kk = 1:ctest, C3 = mtimesm(A, 'c', B, 't', true);end, t3 = toc
tic; for oo = 1:ctest, C4 = pagemtimes(A, 'c', B, 't');end, t4 = toc
max(abs(C1(:)-C2(:)))
max(abs(C1(:)-C3(:)))
max(abs(C1(:)-C4(:)))
% on my notebook (i7-8550u), t1 ~17 sec; t2 ~1.3 sec; t3 ~3.8 sec; t4 ~0.39 sec.
% `PAGEMTIMES` is the fast in this case.
Citar como
wfH (2025). mtimesm, an efficient nD matrix multiplication routine. (https://www.mathworks.com/matlabcentral/fileexchange/82099-mtimesm-an-efficient-nd-matrix-multiplication-routine), MATLAB Central File Exchange. Recuperado .
Compatibilidad con la versión de MATLAB
Se creó con
R2020a
Compatible con cualquier versión
Compatibilidad con las plataformas
Windows macOS LinuxEtiquetas
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Descubra Live Editor
Cree scripts con código, salida y texto formateado en un documento ejecutable.