How can I perform multi-dimensional matrix multiplication in MATLAB?

37 ビュー (過去 30 日間)
MathWorks Support Team
MathWorks Support Team 2009 年 6 月 27 日
コメント済み: Haman 2023 年 9 月 21 日
For example, I have a 3-D matrix, "A", of size n1-by-n2-by-p, and a 2-D matrix, "B", of size n2-by-n1. For each page "p", I would like to calculate "B*A(:,:,p)", which should result in a new matrix of size n2-by-n2-by-p.
It is possible to perform this using a FOR loop, but FOR loops are not optimal for MATLAB calculations. Is there a more efficient way to do this?

採用された回答

MathWorks Support Team
MathWorks Support Team 2023 年 9 月 21 日
編集済み: MathWorks Support Team 2023 年 9 月 7 日
For MATLAB versions before R2020b:
The ability to perform multi-dimensional matrix multiplication in MATLAB is not available.
As a workaround,  please use a FOR loop to achieve the intended multi-dimensional matrix multiplication.
For MATLAB versions R2020b and after:
You can use the "pagemtimes" MATLAB function, to perform page-wise (multi-dimensional) matrix multiplication. Please refer to the following documentation page for more information:
For MATLAB versions R2022a and after:
In addition to "pagemtimes", and depending on your workflow, you can further use the "tensorprod" MATLAB function to perform tensor products. Please refer to the following documentation page for more information:
  4 件のコメント
Bruno Luong
Bruno Luong 2023 年 9 月 8 日
@MathWorks Support Team staff "which should result in a new matrix of size n1-by-n1-by-p."
Make it n2-by-n1-by-p.
Haman
Haman 2023 年 9 月 21 日
@Bruno Luong Thank you for pointing out the error. We've updated the example from "B*A(:,:,p)*B" to "B*A(:,:,p)" and the size from "n1-by-n1-by-p" to "n2-by-n2-by-p" to correct the error in size.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMathematics についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by