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
a=np.array([[1,2],[3,4]],[[1,3],[-3,1]]]])
b=np.array([2,-1],[1,1]])
c=np.einsum('ijk, kl->ijl', a, b)
d1 = a[0]@b
d2 = a[1]@b
print(c)
print(d1)
print(d2)
```

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.

2022-09-30 13:52

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=[1,2;3,4];
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=reshape(a2,M*N,M);
% A =
% 1 2
% 3 4
% 1 3
% -3 1
C = A*b;
tC = C';
c0=reshape(tC,M,M,N);
c=permute(c0,[2,1,3])
```

2022-09-30 13:52

Popular Tags

python x 4628
android x 1593
java x 1493
javascript x 1425
c x 924
c++ x 877
ruby-on-rails x 696
php x 692
python3 x 683
html x 656

© 2023 OneMinuteCode. All rights reserved.