Problem 2237. Mmm! Multi-dimensional Matrix Multiplication
You have got a couple of multi-dimensional matrices, A and B. And want to multiply them. For the first 2 dimensions, an ordinary matrix multiplication applies. And in the other dimensions? Well, they just act as parallel worlds. All 2D matrices are multiplied, for every element in the other dimensions. You may assume that the size in the 1st two dimensions allows simple matrix multiplication: A(:,:,1)*B(:,:,1), so size(A(:,:,1),2) == size(B(:,:,1),1), or either A(:,:,1) is a scalar or B(:,:,1) is a scalar. In the other dimensions, the sizes of A and B should be eqaal, size(A,n) == size(B,n), for n>2, or either ndims(A)<n or ndims(B)<n, or either size(A,n)==1 or size(B,n)==1, so one of them is a scalar.
Write a function mtimesm that does this, and ask Mathworks to include it in the elmat toolbox of the Next Release.
Solution Stats
Problem Comments
-
4 Comments
I gave the solution which passes all given test cases, but I am sure it will fail, if there are multiple combinations of sizes of A and B.
I believe, to get a full-proof solution, more test cases are needed. Or such solution can work for limited combination of sizes of multidimensional matrices to be multiplied.
José Ramón, You are absolutely right. I will fix it.
Have a look in 20b! :)
Solution Comments
Show commentsGroup

Cody Problems in Japanese
- 16 Problems
- 272 Finishers
- ベクトルのスケーリング
- 二つのベクトルの要素ごとの積の平均を計算しよう
- 2倍してみよう - ここからスタート!
- ベクトル [1 2 3 4 5 6 7 8 9 10] の作成
- サイコロを作ろう
- チェッカーボードを作ろう
- ベクトルの値が増加しているかを調べよう
- 二乗になっている数を見つけよう
- 二つのベクトルの要素ごとの積の平均を計算しよう
- 行列内の素数の平均をとろう
- ゼロでない要素が一番多い行を探そう
- 特定の値がベクトル内に含まれているかを確認するコードを書こう
- NaN (欠損値) が含まれている行を削除しよう
- テレビのサイズを求めてみよう
- 英語の文章内の母音を取り除くコードを書きましょう。
- 対称で、n*2n のサイズの行列を作成しましょう
- 文字列の最初と最後の文字だけ抜き出しましょう。
Problem Recent Solvers29
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!