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

Thread Subject:
MTIMESX when Inner matrix dimensions are not agree.

Subject: MTIMESX when Inner matrix dimensions are not agree.

From: ghasem

Date: 17 Apr, 2013 22:10:10

Message: 1 of 12

Hi.
I want to use from MTIMEXS method ( by James Tursa) to multiply 3d arrays,when Inner matrix dimensions are not agree.
for example I have:
A=rand(33,32,32);
B=rand(33,32,32);
So,if I use from " mtimesx(A,B) , I will get this error:
??? Error using ==> mtimesx
Inner matrix dimensions must agree.

while,I used from Point by point multiplication method,previously.i.e:
C = A.*B
============
can I use from "mtimesx" or "mmx" methods in this case?
best wishes...
ghasem

Subject: MTIMESX when Inner matrix dimensions are not agree.

From: ghasem

Date: 18 Apr, 2013 06:07:10

Message: 2 of 12

there is not any idea?

Subject: MTIMESX when Inner matrix dimensions are not agree.

From: Bruno Luong

Date: 18 Apr, 2013 06:50:09

Message: 3 of 12

"ghasem " <shaban_sadeghi@yahoo.com> wrote in message <kko2ie$bm2$1@newscl01ah.mathworks.com>...
> there is not any idea?

mtimesx performs matrix multiplication, not pointwise.

Bruno

Subject: MTIMESX when Inner matrix dimensions are not agree.

From: ghasem

Date: 18 Apr, 2013 07:50:10

Message: 4 of 12

> mtimesx performs matrix multiplication, not pointwise.
>
> Bruno
==============
yes,I know.But I want to know that is there any fast method for element by element multiplication of two 3d arrays?
ghasem

Subject: MTIMESX when Inner matrix dimensions are not agree.

From: Nasser M. Abbasi

Date: 18 Apr, 2013 08:10:52

Message: 5 of 12

On 4/18/2013 2:50 AM, ghasem wrote:
>> mtimesx performs matrix multiplication, not pointwise.
>>
>> Bruno
> ==============


> yes,I know.But I want to know that is there any fast method for element by element
>multiplication of two 3d arrays?
> ghasem
>

What is wrong with just using .* ?

EDU>> help .*

" .* Array multiply.
     X.*Y denotes element-by-element multiplication.
     X and Y must have the same dimensions"


EDU>> A=rand(2,2,2);
EDU>> B=rand(2,2,2);
EDU>> A.*B

--Nasser

Subject: MTIMESX when Inner matrix dimensions are not agree.

From: ghasem

Date: 18 Apr, 2013 08:46:09

Message: 6 of 12

> EDU>> help .*
>
> " .* Array multiply.
> X.*Y denotes element-by-element multiplication.
> X and Y must have the same dimensions"
>
>
> EDU>> A=rand(2,2,2);
> EDU>> B=rand(2,2,2);
> EDU>> A.*B
>
> --Nasser
>
>=================================
I'm familiar with .* operator.
I want to know that this method is best and fastest?
also,I have one question:
how can I use from single-threade BLAS for mmx in matlab?
i.e,how link single-threaded BLAS to matlab to use from it in mmx?

Subject: MTIMESX when Inner matrix dimensions are not agree.

From: Bruno Luong

Date: 18 Apr, 2013 10:22:09

Message: 7 of 12

"ghasem " <shaban_sadeghi@yahoo.com> wrote in message <kko8ji$qlk$1@newscl01ah.mathworks.com>...
> > mtimesx performs matrix multiplication, not pointwise.
> >
> > Bruno
> ==============
> yes,I know.

No you didn't, since you ask about "matrix dimension must agree" error.

And you start over again with mmx.

Bruno

Subject: MTIMESX when Inner matrix dimensions are not agree.

From: ghasem

Date: 18 Apr, 2013 12:28:09

Message: 8 of 12

> No you didn't, since you ask about "matrix dimension must agree" error.
>
> And you start over again with mmx.
>
> Bruno
=================================
yes,I know,but you don't understand my question.
in fact,my question was as follow:
if I want to use from MTIMESX for fast opertaion,but second dimension of A is not equal with first diemnsion of B,but second and third dimensions in both A and B are same,is a good choice MTIMESX?
for example:
a=rand(33,32,32);
b=rand(33,32,32);
I konw that : c = mtimesx(a,b) => is false!
also,I know that simply : c =a.*b
but I want to use from:
c = permute(mtimesx(permute(a,[2 3 1]),permute(b,[2 3 1])),[3 1 2]);
NOW,my question was:
between " .* " operator and " mtimesx (or mmx)" , which is faster?
ghasem

Subject: MTIMESX when Inner matrix dimensions are not agree.

From: Steven_Lord

Date: 18 Apr, 2013 13:57:02

Message: 9 of 12



"ghasem " <shaban_sadeghi@yahoo.com> wrote in message
news:kkoosp$a0m$1@newscl01ah.mathworks.com...
>> No you didn't, since you ask about "matrix dimension must agree" error.
>>
>> And you start over again with mmx.
>>
>> Bruno
> =================================
> yes,I know,but you don't understand my question.
> in fact,my question was as follow:
> if I want to use from MTIMESX for fast opertaion,but second dimension of A
> is not equal with first diemnsion of B,but second and third dimensions in
> both A and B are same,is a good choice MTIMESX?
> for example:
> a=rand(33,32,32);
> b=rand(33,32,32);
> I konw that : c = mtimesx(a,b) => is false!
> also,I know that simply : c =a.*b
> but I want to use from:
> c = permute(mtimesx(permute(a,[2 3 1]),permute(b,[2 3 1])),[3 1 2]);

Why??? Why do this when TIMES does the same operation without performing
three permutations (each of which shuffles memory around and so INCREASES
the time required for the operation?)

> NOW,my question was:
> between " .* " operator and " mtimesx (or mmx)" , which is faster?

I have not used MTIMESX but I would expect "c = a.*b" to be faster than
three calls to PERMUTE and a call to MTIMESX, and my suspicion is that holds
for any matrix size. But if you want a definitive answer on your machine
with your data, time the operations yourself using TIC and TOC or the TimeIt
function from the MATLAB Central File Exchange. In addition, think about the
you from six months from now. Which of these two lines of code will take
that you less time to understand?

c =a.*b;

c = permute(mtimesx(permute(a,[2 3 1]),permute(b,[2 3 1])),[3 1 2]);

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: MTIMESX when Inner matrix dimensions are not agree.

From: ghasem

Date: 18 Apr, 2013 14:28:10

Message: 10 of 12

> In addition, think about the
> you from six months from now. Which of these two lines of code will take
> that you less time to understand?
>
> c =a.*b;
>
> c = permute(mtimesx(permute(a,[2 3 1]),permute(b,[2 3 1])),[3 1 2]);
>
> --
> Steve Lord
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com
============================
I know that .* is simpler and faster.I measured three method and .* is very faster than two other method.my question was wrong.
in fact I want to know that is there any method better than .* operator in 3d?
ghasem

Subject: MTIMESX when Inner matrix dimensions are not agree.

From: Bruno Luong

Date: 18 Apr, 2013 15:24:15

Message: 11 of 12

"ghasem " <shaban_sadeghi@yahoo.com> wrote in message <kkoosp$a0m$1@newscl01ah.mathworks.com>...
>
> if I want to use from MTIMESX for fast opertaion,but second dimension of A is not equal with first diemnsion of B,but second and third dimensions in both A and B are same,is a good choice MTIMESX?
> for example:
> a=rand(33,32,32);
> b=rand(33,32,32);
> I konw that : c = mtimesx(a,b) => is false!
> also,I know that simply : c =a.*b
> but I want to use from:
> c = permute(mtimesx(permute(a,[2 3 1]),permute(b,[2 3 1])),[3 1 2]);
> NOW,my question was:
> between " .* " operator and " mtimesx (or mmx)" , which is faster?

Sorry, be honest. Just admit but you don't know what is difference between matrix multiplication and pointwise multiplication.

>> a=reshape(1:8,[2 2 2])

a(:,:,1) =

     1 3
     2 4


a(:,:,2) =

     5 7
     6 8

>> b=reshape(1:8,[2 2 2])

b(:,:,1) =

     1 3
     2 4


b(:,:,2) =

     5 7
     6 8

>> c = a.*b

c(:,:,1) =

     1 9
     4 16


c(:,:,2) =

    25 49
    36 64

>> d = mtimesx(a,b)

d(:,:,1) =

     7 15
    10 22


d(:,:,2) =

    67 91
    78 106

>> e = permute(mtimesx(permute(a,[2 3 1]),permute(b,[2 3 1])),[3 1 2])

e(:,:,1) =

    16 24
    28 40


e(:,:,2) =

    40 64
    60 88

% None of the results match.

Many be you should refrain asking queston until the difference is absolutly clear in your mind.

Bruno

Subject: MTIMESX when Inner matrix dimensions are not agree.

From: ghasem

Date: 18 Apr, 2013 18:19:19

Message: 12 of 12

you are right Bruno.
thank you very much for your attention...
my project is difficult a little and I'm confused...
ghasem

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us