Is it possible to calculate a gradient via Java Snippet?

Hello :slight_smile:

basically I want to do something very easy namely to calculate a gradient that is

column.value[i] - column.value[i+1]

with the moving aggregation node I found a close solution by setting the window length to 2 and setting the aggreagtion to “Range”. Only problem is that it will always calculate the difference between the largest and smallest element of the group.

So I thought I will write some code in a JavaSnippet but it just wont work. I don’t really know the correct syntax in this snippet.

Something like

Double output_vectorCa = new Double [$input_vector$.length];

for (int i = 0; i < $input_vector$.length; i++){
output_vectorCa[i] =$input_vector$[i] - $input_vector$[i+1]
output_vector = ;output_vectorCa

is not working.

Could someone help me? :slight_smile:

Hi @EngiP3,

to get the largest and smallest value of a group you can use the groupby node followed by the math formula node.


Hi @EngiP3,

You could also use the Lag Column node to shift the column by one, followed by the Math Formula node to subtract the shifted and original columns from another.


1 Like

Hey morpheus! Thanks for your reply but actually that is not what I’m looking for :slight_smile:

Hi Marcel!

Yes that would work thank you! :slight_smile:

But I actually found a solution via JavaSnippet.
I always forget that KNIME works Row-based. Therefore the JavaSnippet calls the method function consecutively for every row.

I worked with a very simple code that is:

variable declaration: double a = 0;

out_output =c_F - a;
a = c_F;

Hi @EngiP3,

if i understood you correctly you try to doe the calculation based on specific groups. Therefore you first have to sort your data in right manner and then check within your statement if the group is different to the previous row. Otherwise you get a wrong value for the first row within a group.


Hi @morpheus,

no no. That was just an example how I semi solved it before. I created groups of 2 to then aggregate via Range.
But what I really wanted to do is to build the difference Row_1 - Row_2 of one Column, to which I found a solution :slight_smile:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.