I have a nx1 vector and a 1xn vector. I want to add them in a special manner like matrix multiplication in an efficient manner (vectorized):
Example:
You can use the repmat
function (replicate matrices):
repmat(A,1,3)+repmat(B,3,1)
From R2016b you can simply do:
A=[1 2 3]'
B=[4 5 6]
A+B
ans =
5 6 7
6 7 8
7 8 9
Matlab will silently expand both vectors and do the element wise sum. This feature has not been without it's controversy. You can check the details here:
Matlab expands arithmetic
You can use bsxfun:
A=[1 2 3]'
B=[4 5 6]
bsxfun(@plus, A, B)
The result is
ans =
5 6 7
6 7 8
7 8 9