Help on Java Array seems not working

Dear All,

I'm using a Java Snippet node to calculate a Tanimoto similarity for continuous values of two vector objects, i.e. a and b.

The continuous calculation requires the summation of all elements in the vectors divided by magnitude of vector a added to the magnitude of vector b, minus the summation of all vectors.

Say if my vectors have 3 elements each, then for the final loop, the variable 'a' should stores the values for all 3 elements. That goes the same with variable 'b' and 'ab'.

However, I found out that the variables only store the last and the previous value of elements. Meaning, the second and the third. Looks like it excludes the first elements. 

I've embedded my codes below and appreciate if anyone can help me to figure out why. Is it the Java Snippet bug of is it my code? 


double a = 0.0;
double b = 0.0;
double ab = 0.0;
double result = 0.0;
Double [] result_array = new Double [vector_a.length];

for (int i = 0; i < vector.length; i++)
	a += Math.pow(vector_a[i],2);
	b += Math.pow(vector_b[i],2);
	ab += vector_a[i] * vector_b[i];
	result = ab / (a + b - ab);
	result_array[i] = result;


Thanks in advanced.



can you post a mini Workflow demonstrating your problem?

Best, Iris

Hi Iris, 

I've solved my problem already. There's nothing wrong with the coding but apparently it is with my dataset.

Thanks for your concern by the way.