# Matrix operation between 2D and 3D (or 4D) matrices

I want to know if there is a specific linear algebra way to represent the following operation.
There is matrix $$X \in \mathbb{R}^{n\times m}$$.
There is a 3D matrix $$A \in \mathbb{R}^{n\times m \times l}$$ where $$l = n*m$$.
I want to multiply $$x_{i,j} \in X$$ to all $$n\times m$$ elements in one 'layer' of $$A$$ and then sum all the $$a_{i,j,\cdotp}$$ of the different layers of $$A$$. This should result in a matrix $$B \in \mathbb{R}^{n\times m}$$.
For example:
$$X = \begin{bmatrix} 2 & 4 \\ 3 & 5 \end{bmatrix}$$,
$$A[\cdotp,\cdotp,1] = \begin{bmatrix} 1 & 0 \\ 0 & 0 \end{bmatrix}$$, $$A[\cdotp,\cdotp,2] = \begin{bmatrix} 0 & 1 \\ 0 & 0 \end{bmatrix}$$, $$A[\cdotp,\cdotp,3] = \begin{bmatrix} 1 & 1 \\ 0 & 0 \end{bmatrix}$$, $$A[\cdotp,\cdotp,4] = \begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix}$$

$$2*A[\cdotp,\cdotp,1] = \begin{bmatrix} 2 & 0 \\ 0 & 0 \end{bmatrix}$$
$$4*A[\cdotp,\cdotp,2] = \begin{bmatrix} 0 & 4 \\ 0 & 0 \end{bmatrix}$$
$$3*A[\cdotp,\cdotp,3] = \begin{bmatrix} 3 & 3 \\ 0 & 0 \end{bmatrix}$$
$$5*A[\cdotp,\cdotp,4] = \begin{bmatrix} 5 & 0 \\ 0 & 5 \end{bmatrix}$$
And then, sum the layers element wise:
$$B = \begin{bmatrix} 2 & 0 \\ 0 & 0 \end{bmatrix} + \begin{bmatrix} 0 & 4 \\ 0 & 0 \end{bmatrix} + \begin{bmatrix} 3 & 3 \\ 0 & 0 \end{bmatrix} + \begin{bmatrix} 5 & 0 \\ 0 & 5 \end{bmatrix} = \begin{bmatrix} 10 & 7 \\ 0 & 5 \end{bmatrix}$$

I'm thinking that $$A$$ could be defined as a 4-dimmensional matrix $$\in \mathbb{R}^{n\times m \times n \times m}$$ so that $$x_{i,j}$$ should be multiply to all the elements of $$A_{\cdotp,\cdotp, i,j}$$ .

• If this is a programming question I recommend python. If it is a pure math question I recommend the so called index notation. You can find lots of examples under this name in MSE. It is just a conventient way of writing down what you multiply with what and what you are summing up. Commented Feb 22, 2022 at 18:51