subrutine to multiplication multidimensional matrix

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

 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;
end

11 Comments

comment by agustin darrosa
But there is a problem, for example:
a is a multidimensional matrix with dimensions 2*3*2*2 and b is other multidimensional matrix with dimensions 3*2*2*2
iF you use
c = bsxfun(@minus, a, b)
matlab says:
*Error using ==> bsxfun Non-singleton dimensions of the two input arrays must match each other.*
But this multiplication is correct
any solution??
thanks
How do you know that "the multiplication" is "correct". Please define the expected result, because it cannot be guesses reliably.
I saw this multiplication in an example:
>> a=[ 1 3 5; 2 4 6]; >> a(:,:,1,2)=[ 13 15 17; 14 16 18]; >> a(:,:,2,1)=[ 7 9 11; 8 10 12] >> a(:,:,2,2)=[ 19 21 23; 20 22 24];
>> b=[ 24 21; 23 20; 22 19]; >> b(:,:,1,2)=[ 12 9; 11 8; 10 7]; >> b(:,:,2,1)=[ 18 15; 17 14; 16 13]; >> b(:,:,2,2)=[ 6 3; 5 2; 4 1]; >> I want multiplicate a and b
Thanks for answer
see ADDED part in my answer
"Multiplication" does not really remind me to the "-" operator. Did I loss the point?
i don´t understand you
sorry
Jan
Jan on 18 Mar 2013
Edited: Jan on 18 Mar 2013
@agustin: Do you mean Andrei or me? I think the main problem is, that we do not understand you. I still do not know what you expect as results.
OK, sorry I am spanish and my english is bad
I want to know how to multiply two multidimensional arrays in matlab
@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.
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
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

Sign in to comment.

More Answers (3)

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:
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.

Community Treasure Hunt

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

Start Hunting!