3D matrix multiplication

154 Ansichten (letzte 30 Tage)
Kamuran
Kamuran am 24 Jun. 2011
Kommentiert: James Tursa am 17 Sep. 2020
Hi,
I have two 3D matrices that I need to multiply in a specific way. I need to do for k=1:Z M(:,:,k)=A(:,:,k)*B(:,:,k) end
but I do not want to use for loop because it makes my code run slower and I need to this multiplication for a unsteady flow profile calculation (close to million time steps). Is there a more efficient way to do it?
  4 Kommentare
Steven Lord
Steven Lord am 17 Sep. 2020
We don't do implicit slice expansion, but as of release R2020b we provide the pagemtimes function for this purpose.
James Tursa
James Tursa am 17 Sep. 2020
Finally! Guess I can now stop worrying about updating MTIMESX.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

James Tursa
James Tursa am 24 Jun. 2011
  1 Kommentar
James Tursa
James Tursa am 24 Jun. 2011
P.S. If your 2D slices are small (4x4 or less), be sure to use 'SPEED' or 'SPEEDOMP' mode. This will cause inline code to be used that will run faster than the default BLAS library routine calls.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (4)

Daniel Frisch
Daniel Frisch am 3 Feb. 2020
Matrix multiplication can also be expressed using native Matlab code (times and sum):
% A : (a x c x Z)
% B : (c x b x Z)
Ap = permute(A,[2,1,4,3]); % (c x a x 1 x Z)
Bp = permute(B,[1,4,2,3]); % (c x 1 x b x Z)
M = Ap .* Bp; % (c x a x b x Z)
M = sum(M,1); % (1 x a x b x Z)
M = permute(M,[2,3,4,1]); % (a x b x Z)
  1 Kommentar
James Tursa
James Tursa am 10 Feb. 2020
Note that the permute( ) operations will involve a deep data copy, so this method will run slower than the mex routines.

Melden Sie sich an, um zu kommentieren.


Walter Roberson
Walter Roberson am 24 Jun. 2011
There is no built-in MATLAB support for 3D multiplications. The program James refers to is probably a good choice.
By the way, especially in the releases of the last few years, "for" loops are sometimes faster than vectorization, especially for large matricies.

Kamuran
Kamuran am 24 Jun. 2011
to use this file I need to install a C compiler ? If so can you suggest me one, Because I never used C :) .
Thank you
  3 Kommentare
James Tursa
James Tursa am 24 Jun. 2011
Type the following at the MATLAB prompt:
mex -setup
Then press Enter and see what shows up. If you have a 32-bit system then hopefully a compiler will already be there. If you have a 64-bit system then you will have to install one yourself unfortunately.
Walter Roberson
Walter Roberson am 24 Jun. 2011
I checked back... we do not have any information about the OS or MATLAB version the poster is using.

Melden Sie sich an, um zu kommentieren.


Mr M.
Mr M. am 6 Apr. 2018
I got the following error message:
Error using mtimesx_build (line 120) Unable to compile mtimesx.c
Error in mtimesx (line 271) mtimesx_build;
  2 Kommentare
Mr M.
Mr M. am 6 Apr. 2018
However XCode is installed and licence is accepted (mex -setup)
James Tursa
James Tursa am 6 Apr. 2018
MTIMESX is way overdue for an update (TMW changed mex procedures some time ago). In the meantime, try this or some variation of this modified for your system:
lib_blas = fullfile(matlabroot,'extern','lib',computer('arch'),'microsoft','libmwblas.lib');
mex('mtimesx.c',lib_blas,'-largeArrayDims')

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by