Page-wise matrix multiplication in MATLAB


When we multiply two N-dimensional matrices along each dimension or page of the two matrices, then it is called page-wise matrix multiplication. Page-wise matrix multiplication is mainly performed in the case of 3-dimensional matrices.

Go through this tutorial to learn the methods of performing page-wise matrix multiplication using MATLAB.

What is Page-Wise Matrix Multiplication?

When two N-dimensional matrices are multiplied along each dimension of the two matrices, then this type of matrix multiplication is called the page-wise matrix multiplication. It is basically the element wise multiplication of two 3D matrices along a specific dimension or page in a 3D matrix.

Let us understand this concept with the help of an example.

Consider two 3-dimensional matrices "A" and "B" whose dimensions are $\mathrm{m \: \times \: n \: \times \: p}$. Then, the page-wise multiplication along the third dimension will be performed by multiplying these two matrices for each page or dimension along the third dimension.

The technical explanation of this multiplication is given below −

If A(:, :, i) and B(:, :, i) are the matrices at the $\mathrm{i^{th}}$ page of the matrices A and B respectively. Then, there page-wise multiplication will be defined as follows,

$$\mathrm{C(: \: , \: : \: , \: i) \: = \: A(: \: , \: : \: , \: i) \: ^* \: B(: \: , \: : \: , \: i)}$$

For all i = 1, 2, 3, …, p.

This is all about basics of page-wise matrix multiplication. Let us now discuss how to perform page-wise matrix multiplication using MATLAB.

Page-Wise Matrix Multiplication using MATLAB

In MATLAB, we have the following two built-in functions to perform the page-wise matrix multiplication −

  • pagemtimes(A, B)

  • pagemtimes(A, transpA, B, transpY)

These two functions are used in MATLAB to perform matrix multiplications along the pages of 3D matrices.

Let us now discuss the implementation of these two functions in MATLAB programming.

Perform Page-Wise Matrix Multiplication of Two 3D Matrices

In MATLAB, the "pagemtimes(A, B)" is a built-in function that is used to perform the page-wise matrix multiplication of two 3-dimensional matrices A and B.

The steps involved in performing this multiplication as explained below −

  • Step 1 − Create two 3D matrices A and B.

  • Step 2 − Call the "pagemtimes()" function with A and B as the arguments to perform their page-wise multiplication.

  • Step 3 − Display the result of page-wise matrix multiplication using the "disp" function.

Example 1

The following example demonstrates the practical implementation of these steps −

% MATLAB code to perform page-wise matrix multiplication
% Create two sample 3D matrices A and B
A = cat(3, [5 5 1; 5 4 3; 2 1 4], [3 5 5; 4 2 3; 2 4 5], [4 2 5; 1 4 2; 1 2 5]);

B = cat(3, [2 3 1; 4 3 1; 2 5 3], [3 2 2; 2 5 3; 1 4 2], [2 5 2; 3 2 5; 1 4 1]);

% Perform page-wise matrix multiplication of A and B
C = pagemtimes(A, B);

% Display the original matrices and their page-wise product
disp('Matrix A:');
disp(A);

disp('Matrix B:');
disp(B);

disp('Page-wise product of A and B:')
disp(C);

Output

It will produce the following output −

Matrix A:
(:, :, 1) =
   5     5     1
   5     4     3
   2     1     4
(:, :, 2) =
   3     5     5
   4     2     3
   2     4     5
(:, :, 3) =
   4     2     5
   1     4     2
   1     2     5

Matrix B:
(:, :, 1) =
   2     3     1
   4     3     1
   2     5     3
(:, :, 2) =
   3     2     2
   2     5     3
   1     4     2
(:, :, 3) =
   2     5     2
   3     2     5
   1     4     1

Page-wise product of A and B:
(:, :, 1) =
   32    35    13
   32    42    18
   16    29    15
(:, :, 2) =
   24    51    31
   19    30    20
   19    44    26
(:, :, 3) =
   19    44    23
   16    21    24
   13    29    17

This example shows the page-wise multiplication of two 3D matrices using the "pagemtimes" function in MATLAB.

Perform Page-Wise Matrix Multiplication between a Matrix and a 3D Matrix

In MATLAB, we can also perform matrix multiplication between a matrix and a 3D matrix using the "pagemtimes(A, B)" function.

Let A is an ordinary matrix and B is a 3D matrix. Then, we can page-wise multiply the matrix A and the matrix B as follows −

  • Step 1 − Create a matrix A and a 3D matrix B.

  • Step 2 − Perform the page-wise multiplication of matrices A and B using the "pagemtimes(A, B)" function.

  • Step 3 − Display the page-wise product of A and B using the "disp" function.

Example 2

Let us understand this multiplication with the help of an example in MATLAB.

% MATLAB code to page-wise multiply a matrix and a 3D matrix
% Create a sample matrix A
A = [5 4 1; 1 5 3; 2 4 1];

% Create a 3D matrix B
B = cat(3, [2 3 1; 4 3 1; 2 5 3], [3 2 2; 2 5 3; 1 4 2], [2 5 2; 3 2 5; 1 4 1]);

% Perform page-wise matrix multiplication of A and B
C = pagemtimes(A, B);

% Display the original matrices and their page-wise product
disp('Ordinary Matrix A:');
disp(A);

disp('3D Matrix B:');
disp(B);

disp('Page-wise product of A and B:')
disp(C);

Output

It will produce the following output −

Ordinary Matrix A:
   5     4     1
   1     5     3
   2     4     1
3D Matrix B:
(:, :, 1) =
   2     3     1
   4     3     1
   2     5     3
(:, :, 2) =
   3     2     2
   2     5     3
   1     4     2
(:, :, 3) =
   2     5     2
   3     2     5
   1     4     1

Page-wise product of A and B:
(:, :, 1) =
   28    32    12
   28    33    15
   22    23     9
(:, :, 2) =
   24    34    24
   16    39    23
   15    28    18
(:, :, 3) =
   23    37    31
   20    27    30
   17    22    25

This example show how we can page-wise multiple an ordinary matrix and a 3D matrix using the "pagemtimes" function in MATLAB.

Perform Page-Wise Matrix Multiplication with Transposed Pages of 3D Matrix

In MATLAB, there is a function "pagemtimes(A, transpA, B, transpB)" which is used to perform page-wise matrix multiplication with transposed pages of the 3D matrices.

These steps are to be followed to perform the page-wise matrix multiplication with transpositions of pages −

  • Step 1 − Create two 3D matrices A and B.

  • Step 2 − Define the transposition flags for the multiplication. These can be "none", "ctranspose", or "transpose".

  • Step 3 − Perform the page-wise matrix multiplication with transposition using the "pagemtimes" function.

  • Step 4 − Display the page-wise product of matrices using the "disp" function.

Example 3

Let us see an example to understand this multiplication in MATLAB.

% MATLAB code to page-wise multiply two 3D matrices with transpositions
% Create two sample 3D matrices A and B
A = cat(3, [5 5 1; 5 4 3; 2 1 4], [3 5 5; 4 2 3; 2 4 5], [4 2 5; 1 4 2; 1 2 5]);

B = cat(3, [2 3 1; 4 3 1; 2 5 3], [3 2 2; 2 5 3; 1 4 2], [2 5 2; 3 2 5; 1 4 1]);

% Specify the transposition flags
transpA = 'none';
transpB = 'transpose';

% Perform page-wise matrix multiplication with transpositions
C = pagemtimes(A, transpA, B, transpB);

% Display the original matrices and their page-wise product
disp('Matrix A:');
disp(A);

disp('Matrix B:');
disp(B);

disp('Page-wise product of A and B with transpositions:')
disp(C);

Output

It will produce the following output −

Matrix A:
(:,:,1) =
   5     5     1
   5     4     3
   2     1     4
(:,:,2) =
   3     5     5
   4     2     3
   2     4     5
(:,:,3) =
   4     2     5
   1     4     2
   1     2     5

Matrix B:
(:,:,1) =
   2     3     1
   4     3     1
   2     5     3
(:,:,2) =
   3     2     2
   2     5     3
   1     4     2
(:,:,3) =
   2     5     2
   3     2     5
   1     4     1

Page-wise product of A and B with transpositions:
(:,:,1) =
   26    36    38
   25    35    39
   11    15    21
(:,:,2) =
   29    46    33
   22    27    18
   24    39    28
(:,:,3) =
   28    41    17
   26    21    19
   22    32    14

This example shows how to perform the page-wise matrix multiplication of two 3D matrices with transpositions.

Conclusion

In conclusion, page-wise matrix multiplication is a matrix multiplication which is usually used to multiply two 3D matrices along a specific dimension. In MATLAB, we have a function "pagemtimes" that is used to perform the page-wise matrix multiplication of two N-dimensional matrices. In this tutorial, I explained the steps involved in performing the page-wise multiplication of matrices with the help of examples in MATLAB.

Updated on: 26-Oct-2023

54 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements