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

New to MATLAB?

adding matrix to every column in other matrix

Asked by Ahmed almansory

Ahmed almansory

on 14 Apr 2013
Accepted Answer by Cedric Wannaz

Cedric Wannaz

hi all i have two matrix a 16x1024 and b 16x1 how i can adding b to every column in a ? thanks

0 Comments

Ahmed almansory

Ahmed almansory

Products

No products are associated with this question.

2 Answers

Answer by Cedric Wannaz

Cedric Wannaz

on 14 Apr 2013
Edited by Cedric Wannaz

Cedric Wannaz

on 14 Apr 2013
Accepted answer
 b_ext = repmat(b, 1, size(a, 2)) ;
 c = a + b_ext ;

0 Comments

Cedric Wannaz

Cedric Wannaz

Answer by James Tursa

James Tursa

on 14 Apr 2013
bsxfun(@plus,a,b)

2 Comments

Cedric Wannaz

Cedric Wannaz

on 14 Apr 2013

+1

I don't understand why it is slower than REPMAT though in cases where size(a,2) is not too large .. I thought that it would kill repmat when I tested.

James Tursa

James Tursa

on 14 Apr 2013

I haven't tested bsxfun much, but I am not surprised by the timing. I have read many accounts here for various problems and several times it has come up that explicit formulations in one way or another beat bsxfun for speed. For small problems like this one the timing is likely irrelevant anyway.

James Tursa

James Tursa

Contact us