matrix product for each element of a multidimensional array in MATLAB

Asked 5 months ago, Updated 5 months ago, 14 views

I would like to achieve matrix product operations for each of the following elements in MATLAB that Python numpy.einsum can achieve.

import numpy as np
c=np.einsum('ijk, kl->ijl', a, b)
d1 = a[0]@b
d2 = a[1]@b

In other words,
for 3D array A (size: MxMxN) and 2D array B (size: MxM). I would like to efficiently calculate the 3D array C satisfying C[:,:,n]=A[:,:,n]*B for each n=1,...,N in MATLAB.
We are currently in a loop regarding n, so please let us know if there is a more efficient way.
Other than using einsum's MATLAB implementation in File Exchange of Github and MathWorks.

python numpy matlab

2022-09-30 13:52

1 Answers

If there is a 3D array A (size: MxMxN) and a 2D array B (size: MxM), wouldn't it be better to change A to M*NxM and then take the product of the normal matrix?

a(:, :, 2) = [1,3;-31];
b = [2, -1; 1,1];

M = size(a,1);
N = size(a,3);
a2 = permit(a,[1,3,2]);
% A =
%      1     2
%      3     4
%      1     3
%     -3     1

C = A*b;

tC = C';

2022-09-30 13:52

If you have any answers or tips

© 2023 OneMinuteCode. All rights reserved.