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:
multiply a 2D matrix with a 3D matrix and get a vector

Subject: multiply a 2D matrix with a 3D matrix and get a vector

From: April Atoufi

Date: 23 Aug, 2012 19:54:08

Message: 1 of 3

Hi,
I have below matrices and I need a vector answer. But I don't know if this is possible or not! I'll appreciate any help.

A is 5x50
B is 3x5x50
I need Answer=A*B to be 3x1

Thanks,
April

Subject: multiply a 2D matrix with a 3D matrix and get a vector

From: James Tursa

Date: 23 Aug, 2012 20:48:07

Message: 2 of 3

"April Atoufi" <atoufi@gmail.com> wrote in message <k161ov$5fc$1@newscl01ah.mathworks.com>...
> Hi,
> I have below matrices and I need a vector answer. But I don't know if this is possible or not! I'll appreciate any help.
>
> A is 5x50
> B is 3x5x50
> I need Answer=A*B to be 3x1
>
> Thanks,
> April

You will need to be more explicit, since a direct matrix multiply will not work obviously. E.g., you could do this:

sum(mtimesx(B,reshape(A,5,1,50)),3)

Or you could do dozens of other calculations to get a 3x1 result. We have no idea which of these you want. Maybe give an example of sample inputs and and output?

James Tursa

Subject: multiply a 2D matrix with a 3D matrix and get a vector

From: Martti

Date: 5 Jul, 2013 07:25:10

Message: 3 of 3

Using mtimesx from MATLAB Central might be the cleanest way, but if you want to do it with m-files only, you could try something like this:

>> size(A)
ans =
           5 1001

>> size(B)
ans =
           3 5 1001

>> res = bsxfun(@times, reshape(B, 3, 5005), reshape(A, 1, 5005));
>> res = reshape(res, [3 5 1001]);
>> res = sum(sum(res,2),3); size(res)
ans =
     3 1

Tags for 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