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.
The 5th case should be: C_correct = cat(3,A(:,:,1)*B,A(:,:,2)*B,A(:,:,3)*B); ??
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.
3565 Solvers
3429 Solvers
Project Euler: Problem 10, Sum of Primes
576 Solvers
Getting the absolute index from a matrix
181 Solvers
Given a window, how many subsets of a vector sum positive
688 Solvers