I am trying to find the absolute difference between every element of one array and every element of another to form a matrix.
I have achieved this using for loops but it is slow and I need it to be faster. I can do it faster in R for example by using the dist method but I am struggling to make it fast in C#.
double[] array1 = new double [] { 1.1, 2.0, 3.0, 4.0, 5.0 };
double[] array2 = new double[] { 6.1, 7.0, 8.0};
double[,] final_array = new double[5, 3];
for (int i = 0; i < 5; i++)
{
for (j = 0; j < 3; j++)
{
final_array[i,j] = Math.Abs(array1[i] - array2[j])
}
}
# expected result of final_array
5 4.1 3.1 2.1 1.1
5.9 5 4 3 2
6.9 6 5 4 3
Although this result is the correct answer I want to do this faster as I will need to do this calculation for arrays of up to 15,000 in size.