What is the syntax to add v1
to each vector in v2
? I know it has to be simple, but I really have searched and not found it.
v1 = {a, b, c}
v2 = {{d, e, f}, {g, h, i}, {j, k, l}}
i.e., sum them in a way to give:
{{a + d, b + e,c + f}, {a + g, b + h, c + i}, {a + j, b + k, c + l}}
Answer
I recommend using Transpose
twice since it is more efficient than other approaches. Moreover Plus
has the Listable
attribute, thus one need not map Plus
over a list (vector).
Transpose[v1 + Transpose[v2]]
{{a + d, b + e, c + f}, {a + g, b + h, c + i}, {a + j, b + k, c + l}}
Having said that remember that one can rewrite it very concisely in the Front-End: Esc tr Esc
:
Comments
Post a Comment