MATLAB: Afficient way to create “sum matrix”

sparse

Hi,
So i would like to implement a function that get a vector as input lets :
g=[g1, g2, g3]
and return a matrix that contain a sum of pairs: so for g , the output matrix G contain 9 elements the G(i,j)=g(i)+g(j)
The problem is that g will be very large vector.
Does anyone know an efficient way to do that with sparse matrices
Thanks

Best Answer

g = 1:3;
m = g+g(:)
m = 3×3
2 3 4 3 4 5 4 5 6