 Tags IOS SQL HTML C RUBY-ON-RAILS MYSQL ASP.NET DEVELOPMENT RUBY .NET LINUX SQL-SERVER REGEX WINDOWS ALGORITHM ECLIPSE VISUAL-STUDIO STRING SVN PERFORMANCE APACHE-FLEX UNIT-TESTING SECURITY LINQ UNIX MATH EMAIL OOP LANGUAGE-AGNOSTIC VB6 # Generating a pairwise 'distance' matrix

By : Mohan Phatak
Date : October 18 2020, 08:10 PM
it helps some times This is a cumulative distance, so take a cumulative sum and then do the distance calculation: code :
``````mat <- c(3,10,7,9)
dist(cumsum(c(0,mat)))
#   1  2  3  4
#2  3
#3 13 10
#4 20 17  7
#5 29 26 16  9
``````

## how to transfer the pairwise distance value in a distance matrix

By : Immanuel Voki Amoako
Date : March 29 2020, 07:55 AM
I wish did fix the issue. Based on the comments, you know how to get the data into the form of an N x 3 matrix, called X, where X(:,1) is the first index, X(:,2) is the second index, and X(:,3) is the corresponding distance.
Let's assume that the indices (id1... idx) are arbitrary numeric labels.
code :
``````% First, build a list of all the unique indices
indx = unique([X(:,1); X(:,2)]);
Nindx = length(indx);

% Second, initialize an empty connection matrix, C
C = zeros(Nindx, Nindx);  %or you could use NaN(Nindx, Nindx)

% Third, loop over the rows of X, and map them to points in the matrix C
for n = 1:size(X,1)
row = find(X(n,1) == indx);
col = find(X(n,2) == indx);
C(row,col) = X(n,3);
end
``````

## Pairwise distance two matrix R

By : anbang
Date : March 29 2020, 07:55 AM
like below fixes the issue You can use the proxy package for these problems. By default, proxy::dist considers each row of a matrix or data frame as a single "object".
code :
``````library(proxy)

proxy::dist(mat1, mat2, method="Manhattan")
[,1]  [,2]  [,3]
[1,]  4804  4832  4656
[2,]  3708  3736  3560
[3,] 17407 17435 17259

proxy::dist(mat1, mat2, method="Euclidean")
[,1]      [,2]      [,3]
[1,]  3397.036  3417.059  3295.962
[2,]  2761.996  2787.495  2708.075
[3,] 12308.674 12328.422 12204.286
``````

## numpy python: vectorize distance function to calculate pairwise distance of 2 matrix with a dimension of (m, 3)

By : altalapita
Date : March 29 2020, 07:55 AM
Pairwise distance means every point in A (m, 3) should be compared to every point in B (n, 3). This results in a (m, n) matrix of distances. With numpy one can use broadcasting to achieve the wanted result. By using A=A[:, np.newaxis, :] and B=B[np.newaxis, :, :] the resulting shapes are A (m, 1, 3) and B(1, n, 3) respectivley. If you then perform a calculation like C = A-B numpy automatically broadcasts. This means you get a copy of all m rows of A for all n columns of B and a copy of all n columns of B for all m rows of A.
code :
``````  A (m, 1, 3)
- B (1, n, 3)
--------------
= C (m, n, 3)
``````
``````import numpy as np
m = 10
n = 12
A = np.random.random((m, 3))
B = np.random.random((n, 3))

# Add newaxis on seconbd axis of A and on first axis on B
# shape: (m, n, 3) = (m, 1, 3) - (1, n, 3)
C = A[:, np.newaxis, :] - B[np.newaxis, :, :]

C = np.linalg.norm(C, axis=-1)
# shape: (m, n)
``````

## Fast Algorithms for Finding Pairwise Euclidean Distance (Distance Matrix)

By : thetouccher
Date : March 29 2020, 07:55 AM
Hope this helps Well, I couldn't resist playing around. I created a Matlab mex C file called pdistc that implements pairwise Euclidean distance for single and double precision. On my machine using Matlab R2012b and R2015a it's 20–25% faster than pdist(and the underlying pdistmex helper function) for large inputs (e.g., 60,000-by-300).
As has been pointed out, this problem is fundamentally bounded by memory and you're asking for a lot of it. My mex C code uses minimal memory beyond that needed for the output. In comparing its memory usage to that of pdist, it looks like the two are virtually the same. In other words, pdist is not using lots of extra memory. Your memory problem is likely in the memory used up before calling pdist (can you use clear to remove any large arrays?) or simply because you're trying to solve a big computational problem on tiny hardware.

## Distance matrix to pairwise distance list in R

By : Taklit
Date : March 29 2020, 07:55 AM
I wish did fix the issue. Is there any R package to obtain a pairwise distance list if my input file is a distance matrix For eg, if my input is a data.frame like this: , If you have a data.frame you could do something like: 