M = mtimesx(A,B);
You can find MTIMESX here:
Answer from James Tursa on mathworks.com
Top answer 1 of 4
3
M = mtimesx(A,B);
You can find MTIMESX here:
2 of 4
2
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)
MathWorks
mathworks.com › matlabcentral › answers › 97028-how-can-i-perform-multi-dimensional-matrix-multiplication-in-matlab
How can I perform multi-dimensional matrix multiplication in MATLAB? - MATLAB Answers - MATLAB Central
June 27, 2009 - The ability to perform multi-dimensional matrix multiplication in MATLAB is not available.
Top answer 1 of 3
5
Use pagemtimes available in R2020b.
https://jp.mathworks.com/help/matlab/ref/pagemtimes.html
2 of 3
0
Some options from the FEX:
MULTIPROD:
MTIMESX: (C-mex code requiring C compiler) needs updated build routine for later MATLAB versions
MMX: (C-mex code requiring C compiler)
And raw times code:
result = sum(bsxfun(@times,matrix1,reshape(matrix2,1,5000,150)),2);
or
result = sum(matrix1.*reshape(matrix2,1,5000,150),2); % later versions of MATLAB
MathWorks
mathworks.com › matlab › mathematics › linear algebra
pagemtimes - Page-wise matrix multiplication - MATLAB
Page-wise functions like pagemtimes operate on 2-D matrices that have been arranged into a multidimensional array. For example, the elements in the third dimension of a 3-D array are commonly called pages because they stack on top of each other like pages in a book. Each page is a matrix that the function operates on.
MathWorks
mathworks.com › matlabcentral › answers › 476862-multiplying-3d-matrices
Multiplying 3D matrices - MATLAB Answers - MATLAB Central
August 20, 2019 - There is no stock function (what a MATRIX LABORATORY doesn't have such feature?), but here it is MTIMESX · Aj on 20 Aug 2019 · Share · Translate · Share a link to this comment · https://www.mathworks.com/matlabcentral/answers/476862-multiplying-3d-matrices#comment_737167 ·
MathWorks
mathworks.com › matlabcentral › answers › 194769-multiplying-along-the-3rd-dimension-of-a-3d-matrix
Multiplying along the 3rd dimension of a 3D matrix - MATLAB Answers - MATLAB Central
March 27, 2015 - I have a 16x16x100 matrix. What is the fastest way to obtain the 16x16 product by multiplying all the 16x16 arrays? Sign in to comment. Sign in to answer this question. ... https://www.mathworks.com/matlabcentral/answers/194769-multiplying-along-the-3rd-dimension-of-a-3d-matrix#answer_172925
Top answer 1 of 10
18
As a personal preference, I like my code to be as succinct and readable as possible.
Here's what I would have done, though it doesn't meet your 'no-loops' requirement:
Use with AIfor m = 1:C
Z(:,:,m) = X(:,:,m)*Y;
end
This results in an A x D x C matrix Z.
And of course, you can always pre-allocate Z to speed things up by using Z = zeros(A,D,C);.
2 of 10
15
You can do this in one line using the functions NUM2CELL to break the matrix X into a cell array and CELLFUN to operate across the cells:
Use with AIZ = cellfun(@(x) x*Y,num2cell(X,[1 2]),'UniformOutput',false);
The result Z is a 1-by-C cell array where each cell contains an A-by-D matrix. If you want Z to be an A-by-D-by-C matrix, you can use the CAT function:
Use with AIZ = cat(3,Z{:});
NOTE: My old solution used MAT2CELL instead of NUM2CELL, which wasn't as succinct:
Use with AI[A,B,C] = size(X);
Z = cellfun(@(x) x*Y,mat2cell(X,A,B,ones(1,C)),'UniformOutput',false);
MathWorks
mathworks.com › matlabcentral › answers › 332898-is-it-possible-to-multiply-a-3d-matrix-with-a-coumn-vector
Is it possible to multiply a 3D matrix with a coumn vector? - MATLAB Answers - MATLAB Central
March 31, 2017 - https://www.mathworks.com/matlabcentral/answers/332898-is-it-possible-to-multiply-a-3d-matrix-with-a-coumn-vector#answer_268600 ... I got confused because some of the dimensions are size 3, but there is also a 3rd dimension, so for generalization's sake:
MathWorks
mathworks.com › matlabcentral › answers › 346075-operation-on-3-dimension-matrix
Operation on 3 dimension matrix - MATLAB Answers - MATLAB Central
June 24, 2017 - https://www.mathworks.com/matlabcentral/answers/346075-operation-on-3-dimension-matrix#comment_464117 ... My answer is for element by element multiplication, like masking an image or something, not row times column "matrix" multiplication.
MathWorks
mathworks.com › matlabcentral › answers › 2173842-elementwise-multiplication-of-two-3d-matrices
Elementwise multiplication of two 3D matrices - MATLAB Answers - MATLAB Central
February 10, 2025 - To multiply two 3D matrices element-wise in MATLAB, the appropriate operator is the dot multiplication operator `.*`, not `pagemtimes`. The `pagemtimes` function is designed for matrix multiplication, which requires that the inner dimensions ...
MathWorks
mathworks.com › matlabcentral › answers › 858545-3d-2d-matrix-multiplication-without-using-a-loop
3D/2D matrix multiplication without using a loop - MATLAB Answers - MATLAB Central
June 17, 2021 - I can calculate this using loops, but my problem is that I have to do this calculation for many sub-matrices (dimension "k" can be up to millions). My current version (see below) takes way to much time, so I'm looking for an alternative way. ... https://www.mathworks.com/matlabcentral/answers/858545-3d-2d-matrix-multiplication-without-using-a-loop#comment_1590285
MathWorks
mathworks.com › matlabcentral › answers › 18335-matrix-products-along-a-three-dimensional-array
matrix products along a three dimensional array - MATLAB Answers - MATLAB Central
October 15, 2011 - Your specific indication that you do not want element-wise multiplication seemed to rule out the corresponding-elements version, but if that _is_ what you want after all, then · prod(A,3) would do that along the third dimension. Sign in to comment. Sign in to answer this question. Mathematics and Optimization Symbolic Math Toolbox Mathematics Number Theory · Find more on Number Theory in Help Center and File Exchange · 3d matrix · Find the treasures in MATLAB Central and discover how the community can help you!