Discover MakerZone

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

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

To resolve issues starting MATLAB on Mac OS X 10.10 (Yosemite) visit:

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?


1 Comment

Andrei Bobrov on 14 Mar 2013

Please give example

agustin darrosa


No products are associated with this question.

4 Answers

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

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;


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


I hope I explained correctly


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]);


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

Andrei Bobrov
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:


James Tursa
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);

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.


Jan Simon
Answer by agustin darrosa on 19 Mar 2013

thanks you!!!


agustin darrosa

Contact us