subrutine to multiplication multidimensional matrix

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

 채택된 답변

Andrei Bobrov
Andrei Bobrov 2013년 3월 14일
편집: Andrei Bobrov 2013년 3월 19일
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

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
Jan
Jan 2013년 3월 18일
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
Jan
Jan 2013년 3월 18일
"Multiplication" does not really remind me to the "-" operator. Did I loss the point?
i don´t understand you
sorry
Jan
Jan 2013년 3월 18일
편집: Jan 2013년 3월 18일
@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
Jan
Jan 2013년 3월 18일
@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
agustin darrosa 2013년 3월 18일
편집: agustin darrosa 2013년 3월 18일
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

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

James Tursa
James Tursa 2013년 3월 15일

1 개 추천

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:
Jan
Jan 2013년 3월 19일
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.

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by