Resolved: Calculating the distance between specific rows contained in an array in R

In this post, we will see how to resolve Calculating the distance between specific rows contained in an array in R

Question:

I’m trying to calculate the distance between specific points contained in an array in R. My data looks like this:
And for each slice I am trying to calculate the distance between the points contained in row [1,] and row [11,] (the first and last points).
I have truly gotten nowhere with this (I’ve tried the dist function and the geomorph::interlmkdist function) so any help would be much appreciated. I most recently tried the usedist::dist_subset function but it showed the following error, ‘Error in as.matrix(d)[idx, idx] : no ‘dimnames’ attribute for array’.
I’ve had success in using the distancePointToPoint function but have to manually input the values from rows [1,] and [11,] which given the extent of the array, is not ideal.
Ideally, I want to return an array that looks something like this:
Thank you!

Best Answer:

You can use apply along the third margin to apply the distance operation to each slice of your array. This is just a simple Euclidian distance function between the first and 11th rows. The output is a named vector:

If you have better answer, please add a comment about this, thank you!

Source: Stackoverflow.com