0
votes

So I have a rather complex (at least for me) problem in R.

I want to calculate distances between two pair of distributions, for nearly 10k pairs.

I have a distance function from package philentropy, which takes two vectors x y and calculates the distance between them such as:

d <- distance(x, y, method="desired_method")

Another option is to create a matrix with each row representing a distribution, so that the function will calculate all pairwise distances among all distributions in the matrix:

d <- distance(x, method="desired_method")

I have two correlation matrices a and b with nearly 10k rows each, corresponding to 10k correlation distributions. Both matrices have the same number of rows, and my goal is to contrast first row of matrix a with first row of matrix b, second a row with second b row and so on, iteratively.

I can select each desired rows and perform the first distance usage, or I can merge the two matrices with rbind and perform all pairwise distances with second distance usage.

The problem is, with first approach, I do not know how to generate a for loop to iteratively get the nth row of each matrix, and perform distance calculation, while storing the result in a vector.

Additionally, if I perform the second option, I do not want to get all pairwise distances, but just distances corresponding to:

d[i,i+nrow(a)]

And doing so iteratively to generate a corresponding vector of nrow(a) values.

Any help?

1
To get the nth row of a matrix named ‘mat’, you use the syntax: mat[n,]Joe
I don't see a y argument in philentropy::distance.Parfait

1 Answers

0
votes

If you have two matrices, mat_x and mat_y, each with the same number of rows, then the for loop would be:

answer <- vector(mode = 'numeric', length = 10000L)

for (i in 1:10000){
  answer[[i]] <- distance(mat_x[i,], mat_y[i,], method="desired_method")
}