MATLAB Answers


Vector and matrices dotwise multiplication

Asked by Piment
on 8 Apr 2013

I have a n elements vector a1 a2 ... an

and n same-sized matrices b1 b2 ... bn

how can I do a1*b1 + a2*b2 + ... + an*bn without loop? I was thinking if there's a way to do something like multidimentsional array dotwise multiplication? Thanks.



No products are associated with this question.

1 Answer

Answer by per isakson
on 8 Apr 2013


    a * transpose( b )


    a = [ a1, a2, ... an ]
    b = [ b1, b2, ... bn ] 

  1 Comment

I'm not sure if I have made it clear enough, the thing is it's not gonna work because I've tried this before. this is what I did and what matlab spitted out:

>> a = [rand(1), rand(1), rand(1), rand(1)];

>> [b1, b2, b3, b4] = deal(magic(3));

>> b = [b1, b2, b3, b4];

>> a * transpose(b);

Error using *

Inner matrix dimensions must agree.

Join the 15-year community celebration.

Play games and win prizes!

Learn more
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

MATLAB Academy

New to MATLAB?

Learn MATLAB today!