MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

# subrutine to multiplication multidimensional matrix

Asked by agustin darrosa on 14 Mar 2013

Can you tell me a subrutine to multiplication multidimensional matrix in Matlab?

thanks

## 1 Comment

Andrei Bobrov on 14 Mar 2013

## Products

No products are associated with this question.

Answer by Andrei Bobrov on 14 Mar 2013
Edited by Andrei Bobrov on 19 Mar 2013

use bsxfun

ADDED on agustin darrosa's comment [EDIT] ;)

```if numel(a) == numel(b)
c1 = a - permute(b,[2 1 3 4]);
c2 = permute(a,[2 1 3 4]) - b;
end
```

Jan Simon on 18 Mar 2013

@agustin: Welcome in the forum! Here are a lot of non-native speakers and asking questions for clarifications is a usual process for finding solutions.

How do you define the multiplikation of multi-dimensional arrays? This is not a standard procedure in a mathematical sense, so we have to know, what you want as result.

agustin darrosa on 18 Mar 2013

I just want to see the result of the multiplication of matrices

Let a and b multidimensional array. I want to see the resulting c

c=a*b

I hope I explained correctly

thanks

Andrei Bobrov on 19 Mar 2013

Comment by agustin darrosa

Thanks andrei but i have this error:

1. First i define the matrixs a and b

2. then I insert

if numel(a) == numel(b):

c = a - permute(b,[2 1 3 4]);

or

c = permute(a,[2 1 3 4]) - b; ??? if numel(a) == numel(b): | Error: Expression or statement is incomplete or incorrect.

but i have this problem and i don´t know why

Answer by James Tursa on 15 Mar 2013

You haven't given an example so we don't really know what you want yet. Maybe it is this, which does a matrix multiply on the first two dimensions:

http://www.mathworks.com/matlabcentral/fileexchange/25977-mtimesx-fast-matrix-multiply-with-multi-dimensional-support

Answer by Jan Simon on 19 Mar 2013
```A = rand(2,3,2,2);
B = rand(3,2,2,2);
```
```% A simple loop:
R = zeros(2,2,2,2);
for i1 = 1:2
for i2 = 1:2
R(:, :, i1, i2) = A(:, :, i1, i2) * B(:, :, i1, i2);
end
end
```

Is this what you want? If so, note that this is actually a list of DOT products only. Then reshape B to a [3 x 8] array, A to a [8 x 3] (which needs a permute(A, [1,3,4,2]) also) and multiply these matrices. Finally a reshaping give the same R.

Unfortunately I do not dare to post the code, because I do not have Matlab for testing at the moment.

Answer by agustin darrosa on 19 Mar 2013

thanks you!!!