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 日

1 投票

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 件のコメント

Andrei Bobrov
Andrei Bobrov 2013 年 3 月 18 日
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.
agustin darrosa
agustin darrosa 2013 年 3 月 18 日
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
Andrei Bobrov
Andrei Bobrov 2013 年 3 月 18 日
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?
agustin darrosa
agustin darrosa 2013 年 3 月 18 日
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.
agustin darrosa
agustin darrosa 2013 年 3 月 18 日
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
Andrei Bobrov
Andrei Bobrov 2013 年 3 月 19 日
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 日

0 投票

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 ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by