Say I have an array of values:
values = 1:100;
an array of indices:
interval_indices = [40 45 80];
and an array of quantities that I would like to add to the elements in values:
quantities_to_add = [5 -9 30];
I am looking for a compact expression in MATLAB (maybe using accumarray?) that allows me to add the elmements of quantities_to_add to the elements in values depending on the indices specified by indices.
If I were to do this manually:
values(1:interval_indices(1)) = values(1:interval_indices(1)) + ...
quantities_to_add(1);
values(interval_indices(1):interval_indices(2)) = values(interval_indices(1):interval_indices(2)) + ...
quantities_to_add(2);
% and so forth
values(interval_indices(end-1):interval_indices(end)) = values(interval_indices(end-1):interval_indices(end)) + ...
quantities_to_add(end);
EDIT:
Actually, this is a much smarter and vectorized way of doing the same:
Previous answer:
One way of doing it is using
arrayfunto generate a vector of quantities and then adding.