adding matrix to every column in other matrix

 采纳的回答

b_ext = repmat(b, 1, size(a, 2)) ;
c = a + b_ext ;

更多回答(1 个)

bsxfun(@plus,a,b)

2 个评论

+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.
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.

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by