I have a vector of values I need to add to a second vector at indices specified by another vector. How do I accomplish this using Octave/Matlab?
EDIT:
v1 = [1 2 3 4]
v2 = [0 0]
indices = [1 2 1 2]
output = [4 6]
The first and third elements of v1 are added to index 1 of v2, and second and fourth element of v1 are added to second element of v2.
I think this is what you mean (if you provide a small example in your question it’s easier to understand).
You have a vector of values
You have a second, bigger vector:
You want to do
bigVector + toAdd, where you add the elements oftoAddat specific indices intobigVector, specified by:That is, you want the output vector:
In that case, you can do the following:
In particular, notice the
outputVector(indices)andbigVector(indices), which selects the elements ofoutputVectorandbigVectorspecified by the vectorindices.