Login  Register

Subtract consecutive values in an array

Posted by Straub, Volko A. (Dr.) on Oct 20, 2016; 7:12am
URL: http://imagej.273.s1.nabble.com/Subtract-consecutive-values-in-an-array-tp5017426.html

Dear All,

Is there a simple method in a macro to subtract the first value in an
array from the second value, etc? This could obviously done by writing a
for-loop and iterating through the array, but that seems to be a bit
clumsy. Is there a more elegant way that I am missing?

Here is what I try to achieve:
Let's assume, I have the following array [6,9,4,3,5,...]. I would like
to calculate an array that contains the difference between consecutive
elements in the original array, i.e. [3,-5,-1,2, ...] or more generally
[x1-x0, x2-x1, x3-x2, ...]. Obviously the new array would need to be one
element shorter than the original array as there is nothing to subtract
from x0.

Perhaps this could be achieved by shifting all elements in an array by
one index position and then subtract the original array from the shifted
array, but I don't seem to work out whether this is possible within a macro.

Any suggestions/pointers would be very welcome.
Thanks,
Volko

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html