%% Multiply N-D Arrays
% Create a 3-by-3-by-2 array X and multiply it with a 3-by-3-by-1-by-4 array Y. The result has size 3-by-3-by-2-by-4.

X = ones(3,3,2);
A = eye(3);
Y = cat(4,A,2*A,3*A,4*A);
Z = pagemtimes(X,Y)

% Each dimension with size 1 (after the first two dimensions) is implicitly expanded to match the dimension size of the other input, and then each page of the output Z(:,:,i,j) is formed by multiplying X(:,:,i,j)*Y(:,:,i,j). An intuitive way to think about this operation is that X contains two matrices as pages in a 3-D array, and Y contains four matrices arranged along the fourth dimension; thus, multiplying all combinations of these matrices results in eight 3-by-3 matrices.
% Copyright 2020 The MathWorks, Inc.