How to operate with multidimensional arrays (Einstein summation)?
조회 수: 13 (최근 30일)
이전 댓글 표시
Let's imagine that I have an array of dimension 4 (A_ijkl) that I want to multiply with an array of dimension 2 (B_kl) to obtain an array of dimension 2 (C_ij; i.e., summing over indices k and l). What could be the most efficient (and clean) manner of doing so in Matlab?
And let's go one step further, what if we want to do C_ijxy=A_ijklxy * B_klxy ?
Thank you
댓글 수: 0
채택된 답변
Matt J
2017년 12월 5일
편집: Matt J
2017년 12월 5일
You would reshape() to transform it to a 2D matrix multiplication. In the first case, this would be
A=rand(3,4,2,2);
B=rand(2,2);
C=reshape(A,12,4)*B(:);
C=reshape( C ,3,4);
For the latter case, you would have to permute() and reshape()
A=rand(3,4,2,2,6,5); B=rand(2,2,7,8);
Ap=permute(A,[1,2,5,6,3,4]);
Apr=reshape(Ap,[],4); Br=reshape(B,4,[]);
C=reshape(Arp*Br,[3,4,6,5,7,8]);
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!