# How to restrict tensorprod (with contraction) to only give certain results?

9 views (last 30 days)
Eric on 25 Feb 2024
Answered: Matt J on 25 Feb 2024
I have vector of matrices A and a vector of vectors M. I want to do matrix multiplication of matrix in A and corresponding vector in M where the result in in R:
A=((1 2 M=((1 R=((5
3 4) 2) 11)
(5 6 (3 (39
7 8)) 4)) 53))
I have three possible solutions:
A=cat(3,[1 2;3 4],[5 6;7 8]) % vector of matrices
M=cat(3,[1;2],[3;4]) % vector of vectors
% 1
N=permute(M,[2 1 3]) % make rows of the vectors
C=A.*N
R1=sum(C,2)
% 2
R2=pagemtimes(A,M)
% 3
R3=tensorprod(A,M,2,1) % contraction acts as matrix multiplication
This gives:
R1(:,:,1) =
5
11
R1(:,:,2) =
39
53
R2(:,:,1) =
5
11
R2(:,:,2) =
39
53
R3(:,:,1,1) =
5 17
11 23
R3(:,:,1,2) =
11 39
25 53
So R1 and R2 I must reshape, but the solution is there. But I have a question about the tensorprod. Because it calculates four vectors. So can I do something with tensorprod so that it gives the upper left and lower right vectors?

Matt J on 25 Feb 2024
No, you cannot. pagemtimes is the appropriate solution.

### Categories

Find more on Linear Algebra in Help Center and File Exchange

R2023b

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!