The general procedure is called tensor contraction. Concretely it's given by summing over various indices. For example, just as ordinary matrix multiplication is given by

we can contract by summing across any index. For example, we can write

$$c_{ijlm} = \sum_k a_{ijk} b_{klm}$$

which gives a -tensor ("-dimensional matrix") rather than a -tensor. One can also contract twice, for example

which gives a -tensor.

The abstract details shouldn't matter terribly unless you explicitly want to implement mixed variance, which as far as I know nobody who writes algorithms for manipulating matrices does.

Answer from Qiaochu Yuan on Stack Exchange
Top answer
1 of 5
53

The general procedure is called tensor contraction. Concretely it's given by summing over various indices. For example, just as ordinary matrix multiplication is given by

we can contract by summing across any index. For example, we can write

$$c_{ijlm} = \sum_k a_{ijk} b_{klm}$$

which gives a -tensor ("-dimensional matrix") rather than a -tensor. One can also contract twice, for example

which gives a -tensor.

The abstract details shouldn't matter terribly unless you explicitly want to implement mixed variance, which as far as I know nobody who writes algorithms for manipulating matrices does.

2 of 5
9

Sorry to revive the thread, but what I found might answer the original question and help others who might stumble into this in the future. This came up for me when I wanted to avoid using for-loop and instead do one big multiplication on 3D matrices.

So first, let's look how matrix multiplication works. Say you have A[m,n] and B[n,p]. One requirement is that number of columns of A must match the number of rows of B. Then, all you do is iterate over rows of A (i) and columns of B (j) and the common dimension of both (k) (matlab/octave example):

m=2;n=3;p=4;A=randn(m,n);B=randn(n,p);
C=zeros(m,p);
for i = 1:m
    for j = 1:p
        for k = 1:n
             C(i,j) = C(i,j) + A(i,k)*B(k,j);   
        end
    end
end
C-A*B %to check the code, should output zeros

So the common dimension n got "contracted" I believe (Qiaochu Yuan's answer made so much sense once I started coding it).

Now, assuming you want something similar to happen in 3D case, ie one common dimension to contract, what would you do? Assume you have A[l,m,n] and B[n,p,q]. The requirement of the common dimension is still there - the last one of A must equal the first one of B. Then theoretically (this is just one way to do it and it just makes sense to me, no other foundation for this), n just cancels in LxMxNxNxPxQ and what you get is LxMxPxQ. The result is not even the same kind of creature, it is not 3-dimensional, instead it grew to 4D (just like Qiaochu Yuan pointed out btw). But oh well, how would you compute it? Well, just append 2 more for loops to iterate over new dimensions:

l=5;m=2;n=3;p=4;q=6;A=randn(l,m,n);B=randn(n,p,q);
C=zeros(l,m,p,q);
for h = 1:l
    for i = 1:m
        for j = 1:p
            for g = 1:q
                for k = 1:n
                    C(h,i,j,g) = C(h,i,j,g) + A(h,i,k)*B(k,j,g);
                end
            end
        end
    end
end

At the heart of it, it is still row-by-column kind of operation (hence only one dimension "contracts"), just over more data.

Now, my real problem was actually A[m,n] and B[n,p,q], where the creatures came from different dimensions (2D times 3D), but it seems doable nonetheless (afterall matrix times vector is 2D times 1D). So for me the result is C[m,p,q]:

m=2;n=3;p=4;q=5;A=randn(m,n);B=randn(n,p,q);
C=zeros(m,p,q);Ct=C;
for i = 1:m
    for j = 1:p
        for g = 1:q
            for k = 1:n
                C(i,j,g) = C(i,j,g) + A(i,k)*B(k,j,g);
            end
        end
    end
end

which checks out against using the full for-loops:

for j = 1:p
    for g = 1:q
        Ct(:,j,g) = A*B(:,j,g); %"true", but still uses for-loops
    end
end
C-Ct

but doesn't achieve my initial goal of just calling some built-in matlab function to do the work for me. Still, it was fun to play with this.

🌐
GeeksforGeeks
geeksforgeeks.org › python › numpy-3d-matrix-multiplication
NumPy - 3D matrix multiplication - GeeksforGeeks
July 23, 2025 - So, matrix multiplication of 3D matrices involves multiple multiplications of 2D matrices, which eventually boils down to a dot product between their row/column vectors. Here we will see two different examples of matrix multiplication where we have used different dimensions in each example.
Discussions

Can we multiply 3 dimension matrices?
This is where we stop using the term matrix and start using the term tensor. It starts to look like sum P[x,y,z]V[z] over z More on reddit.com
🌐 r/learnmath
13
18
July 9, 2022
3D matrix multiplication - Part 1 (2018) - fast.ai Course Forums
I’m into Lesson 8 - of Intro to ML . I’m not able to wrap my head around 3d matrix multiplication. Multiplying two matrices of size MN and NQ gives M*Q. This is intuitive and I can visualize. I’m not able to do the same for Matrix multiplication of Tensor 3. How does 10000x28x28 by 10x28x28 ... More on forums.fast.ai
🌐 forums.fast.ai
0
January 23, 2018
3D matrix multiplication
3D matrix multiplication. Learn more about 3d matrix multiplication More on mathworks.com
🌐 mathworks.com
5
1
June 24, 2011
Matrix multiplication of 3d arrays
Hi I have a 3d array of dimension 2*5000*150 and another array of dimension 5000*1*150. I would like to take each 2*5000 matrix in the first 3d array and multiply it with the corresponding 5000... More on mathworks.com
🌐 mathworks.com
3
0
May 16, 2018
🌐
Reddit
reddit.com › r/learnmath › can we multiply 3 dimension matrices?
r/learnmath on Reddit: Can we multiply 3 dimension matrices?
July 9, 2022 -

I'm reading these notes on reinforcement learning: https://nbro.gitlab.io/blogging/2022/02/16/bellman-equations/#vectorized-form

The author gives the equation

Q𝜋 = R + 𝛾PV𝜋

Where Q𝜋 and R are defined as matrices in the space of the cartesian product of two sets, which I think makes sense. Also V𝜋 is a vector.

But P is defined as a cartesian product of three sets, meaning that it would be a 3 dimensional matrix right? How do we multiply a vector by a 3D matrix (or how do we multiply 3D matrices at all for that matter)?

🌐
Illinois
rsim.cs.illinois.edu › arch › qual_papers › systems › 11.pdf pdf
A three- dimensional approach to parallel matrix multiplication
Table 2 Performance results for the 3D matrix multiplication algorithm when using DGEMMS [l] for the local call. The · matrices to be multiplied are square of dimension 5000.
🌐
Fast.ai
forums.fast.ai › part 1 (2018)
3D matrix multiplication - Part 1 (2018) - fast.ai Course Forums
January 23, 2018 - Multiplying two matrices of size MN and NQ gives M*Q. This is intuitive and I can visualize. I’m not able to do the same for Matrix multiplication of Tensor 3. How does 10000x28x28 by 10x28x28 ...
🌐
Mlforbeginners
mlforbeginners.com › home › linear algebra › matrix multiplication dimensions: the ultimate guide to size, shape & rules
Matrix Multiplication Dimensions: Size, Shape & Rules
How does the dimension change? The computer uses matrix multiplication dimensions. It multiplies the 3D coordinate vector (1×3) by a projection matrix (3×2).
Published: February 10, 2026
🌐
YouTube
youtube.com › watch
2014 12 27 Three Dimensional Matrix Multiplication - YouTube
Enjoy the videos and music you love, upload original content, and share it all with friends, family, and the world on YouTube.
Published: December 27, 2014
Find elsewhere
🌐
Gamemath
gamemath.com › book › matrixintro.html
Introduction to Matrices - 3D Math Primer for Graphics and Game Development
Section 4.1.1 introduces the concept of matrix dimension and describes some matrix notation. Section 4.1.2 describes square matrices. Section 4.1.3 interprets vectors as matrices. Section 4.1.4 describes matrix transposition. Section 4.1.5 explains how to multiply a matrix by a scalar.
🌐
Tamivox
tamivox.org › redbear › gen_matrix_mult › index.html
generalized multidimensional matrix multiplication
June 14, 2016 - Can matrix multiplication be extended to matrices of three or more dimensions? Of course it can be, and it certainly has been. However, such operations are, relative to the 2-D version, infrequently seen. Moreover, there are various ways that multiplication for n-D matrices might be defined, and no one of them has risen to prominence.
🌐
IEEE Xplore
ieeexplore.ieee.org › document › 5389455
A three-dimensional approach to parallel matrix multiplication | IBM Journals & Magazine | IEEE Xplore
The P processors are configured as a “virtual” processing cube with dimensions p 1 , p 2 , and p 3 proportional to the matrices' dimensions—M, N, and K. Each processor performs a single local matrix multiplication of size M/p 1 × N/p ...
🌐
Calstate
scholarworks.calstate.edu › concern › theses › 00000096t
3-Dimensional Matrix Mathematical Operations | ScholarWorks
April 9, 2020 - This thesis is an attempt to develop mathematically consistent procedures to perform Three-Dimensional (3-D) matrix math. 3-D matrix operations for Right-multiplication, Left-multiplication, Left-Identity, Right-Identity, Left-Inverse, Right-Inverse, and Eigenvalue/Eigenvector matrix are explained.The innovative technique used here is to separate Three-Dimensional (3-D) matrix arrays into multiple Two-dimensional (2-D) matrix arrays to simplify performing 3-D array mathematics.
🌐
YouTube
youtube.com › udacity
Matrix Vector Multiplication - Interactive 3D Graphics - YouTube
This video is part of an online course, Interactive 3D Graphics. Check out the course here: https://www.udacity.com/course/cs291.
Published: February 23, 2015
Views: 1K
🌐
YouTube
youtube.com › watch
Matrix Multiplication for 3D Rendering - YouTube
In this video, I write a function to perform matrix multiplication in Java. This is in anticipation of videos about 3D projection and rotation.🔗 Matrix Mult...
Published: August 20, 2018
🌐
YouTube
youtube.com › watch
Understanding Numpy Matmul in 3D through Examples - YouTube
Here I take you through many examples of Numpy matrix multiplication with combinations of 3D, 2D, and 1D arrays. By the end of the video, you'll have a deep ...
Published: August 23, 2023
🌐
SCIRP
scirp.org › home › open access library journal › vol.4 no.5, may 2017 › 3d matrix ring with a “common” multiplication
3D Matrix Ring with a “Common” Multiplication
May 15, 2017 - In this article, starting from geometrical considerations, he was born with the idea of 3D matrices, which have developed in this article. A problem here was the definition of multiplication, which we have given in analogy with the usual 2D matrices. The goal here is 3D matrices to be a ...