I'm working on implementing a java method based on this correlation formula:
(f * g)[n] = ∑ f[n+m] * g[m],
the lower bound of the summation
is m = 0, and the upper bound is M - 1,
where f is an array of size N
and g is an array of size M.
(f * g)[n] is an array of size N - M + 1
So for example if we have these arrays:
f = {1, 2, 3, 4}, g = {10, 20}
The result is this array:
(f * g)[n] = {1(10) + 2(20), 2(10) + 3(20), 3(10) + 4(20)}
= **{50, 80, 110}**
What I need to do is convert this into Java code. I'm starting by trying to write the code for this example, and then later come up with a more general case. Unfortunately I'm stuck on some of the Java syntax. This is what I currently have:
public static void main(String[] args) {
double[] array1 = new double[] {1, 2, 3, 4, 5};
double[] array2 = new double[] {10, 20};
double[] array3 = new double[3];
for (int i = 0; i < array1.length; i++) {
for (int j = 0; j < array2.length; j++) {
double pos = array1[i];
double multiply = pos * array2[j];
array3[i]= pos * multiply;
}
}
System.out.print(Arrays.toString(array3));
}
I'm fairly certain the problem is with
array3[i] = pos * multiply;
Essentially what I'm trying to do is to store the multiplication in array3's first index. Then as the loop goes on store it into the second and then third index. I know I'm doing it wrong, but I'm not sure how to approach it, and a third for loop seems confusing and impractical (and I'm not even sure if it would work). Thank you!