Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

MTIMESX (Multiplication of 3D matrix with 2D matrix using MTIMESX)

155 views
Skip to first unread message

Sam T

unread,
Jul 28, 2012, 1:49:36 AM7/28/12
to
Hi,

I have been trying to use MTIMESX code to multiply two big n-dimensional matrices (A = 2500x2500x2500 and B = 2500x400) in a loop. But for simplistic purposes, consider the following example:

A = 3 x 3 x 3
B = 3 x 2

and C = A*B;
The multiplication A*B is such that dimension of C = 3 x 3 x 2

where C(i, j, l) = A(i, j, k) * B(k, l);
i.e.
C(:, :, 1) = A(:, :, k) * B(k, 1);
C(:, :, 2) = A(:, :, k) * B(k, 2);

Now, if I use mtimesx(A, B), then resultant matrix is (3 x 2 x 3) as compared (3 x 3 x 2).

This is because first two dimensions specify the matrix multiply involved. In this case it becomes 1st and 2nd dimension of matrix A while it should ideally be 2nd and 3rd dimension of matrix A to get the desired result.

I am wondering if there is a way around it? I look forward to hearing from you.

Thanks for your help.

Bruno Luong

unread,
Jul 28, 2012, 2:48:39 AM7/28/12
to
"Sam T" wrote in message <juvuhg$3s1$1...@newscl01ah.mathworks.com>...
Use function permute() to rearrange data as you prefer.

Bruno

Sam T

unread,
Jul 28, 2012, 5:00:12 AM7/28/12
to
Permute helps in rearranging the data while keeping the same values.

In this case, multiplying A and B can result in completely different values depending on which dimension of A specify the matrix involved. If 1st and 2nd dimension of A specify the matrix involved, entries of resultant matrix C (C=A*B) are compared to the case where 2nd and 3rd dimension of of A specify the matrix involved.


"Bruno Luong" <b.l...@fogale.findmycountry> wrote in message <jv0207$ete$1...@newscl01ah.mathworks.com>...

Bruno Luong

unread,
Jul 28, 2012, 5:15:12 AM7/28/12
to
"Sam T" wrote in message <jv09ms$a7k$1...@newscl01ah.mathworks.com>...
> Permute helps in rearranging the data while keeping the same values.
>
> In this case, multiplying A and B can result in completely different values depending on which dimension of A specify the matrix involved. If 1st and 2nd dimension of A specify the matrix involved, entries of resultant matrix C (C=A*B) are compared to the case where 2nd and 3rd dimension of of A specify the matrix involved.

So apply permute on A. What is the deal?

Bruno

Sam T

unread,
Jul 28, 2012, 5:20:13 AM7/28/12
to
Apologies for the repeated message. Just realized a typo in the previous message. Here we go again.

Permute helps in rearranging the data while keeping the same values. In this case, multiplying A and B can result in different values depending on which dimension of A specify the matrix involved. If 1st and 2nd dimension of A specify the matrix involved, entries of resultant matrix C (C=A*B) are different compared to the case where 2nd and 3rd dimension of of A specify the matrix involved.

"Bruno Luong" <b.l...@fogale.findmycountry> wrote in message <jv0207$ete$1...@newscl01ah.mathworks.com>...

Matt J

unread,
Jul 28, 2012, 7:54:13 AM7/28/12
to
"Sam T" wrote in message <jv0asd$dub$1...@newscl01ah.mathworks.com>...
> Apologies for the repeated message. Just realized a typo in the previous message. Here we go again.
>
> Permute helps in rearranging the data while keeping the same values. In this case, multiplying A and B can result in different values depending on which dimension of A specify the matrix involved. If 1st and 2nd dimension of A specify the matrix involved, entries of resultant matrix C (C=A*B) are different compared to the case where 2nd and 3rd dimension of of A specify the matrix involved.
==============

Yes, but that's what PERMUTE allows you to control. Using PERMUTE, you can put the data you want into the 1st and 2nd dimension of A...

James Tursa

unread,
Jul 28, 2012, 3:59:14 PM7/28/12
to
"Sam T" wrote in message <juvuhg$3s1$1...@newscl01ah.mathworks.com>...
Can you write out a complete MATLAB m-code loop to show what exact computation you are trying to perform? What does k have to with it? I.e., write out the entire computation using m-code loops.

James Tursa
0 new messages