Subtract consecutive values in an array

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Subtract consecutive values in an array

Straub, Volko A. (Dr.)
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
Reply | Threaded
Open this post in threaded view
|

Re: Subtract consecutive values in an array

jerie
Hi Volko,

how about converting the array into an image, duplicate it, add one pixel
to each copy at beginning/end and subtract the images?

Abraços, Jens

Dr. Jens Rietdorf, visiting scientist @ center for technological
development in health CDTS, Oswaldo Cruz Foundation Fiocruz, Rio de Janeiro
Brasil.

On Thu, Oct 20, 2016 at 5:12 AM, Volko Straub <[hidden email]> wrote:

> 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
>

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Jens Rietdorf Visiting Scientist Fundação Oswaldo Cruz - Ministério da Saúde, Centro de Desenvolvimento Tecnológico em Saúde (CDTS), Rio de Janeiro, Brasil.
Reply | Threaded
Open this post in threaded view
|

Re: Subtract consecutive values in an array

Straub, Volko A. (Dr.)
Hi Jens,

On 20/10/2016 13:06, jens rietdorf wrote:
> Hi Volko,
>
> how about converting the array into an image, duplicate it, add one pixel
> to each copy at beginning/end and subtract the images?
Thanks for the suggestion - that is definitely an option and I will give
it a go.
Volko

>
> Abraços, Jens
>
> Dr. Jens Rietdorf, visiting scientist @ center for technological
> development in health CDTS, Oswaldo Cruz Foundation Fiocruz, Rio de Janeiro
> Brasil.
>
> On Thu, Oct 20, 2016 at 5:12 AM, Volko Straub <[hidden email]> wrote:
>
>> 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
>>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html

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