All Questions

Filter by
Sorted by
Tagged with
32 votes
5 answers
61k views

Efficiently Calculating a Euclidean Distance Matrix Using Numpy

I have a set of points in 2-dimensional space and need to calculate the distance from each point to each other point. I have a relatively small number of points, maybe at most 100. But since I need ...
Wes Modes's user avatar
  • 2,074
21 votes
3 answers
42k views

Distance calculation between rows in Pandas Dataframe using a distance matrix

I have the following Pandas DataFrame: In [31]: import pandas as pd sample = pd.DataFrame({'Sym1': ['a','a','a','d'],'Sym2':['a','c','b','b'],'Sym3':['a','c','b','d'],'Sym4':['b','b','b','a']},index=[...
Clayton's user avatar
  • 1,545
13 votes
2 answers
5k views

Efficiently compute pairwise squared Euclidean distance in Matlab

Given two sets of d-dimensional points. How can I most efficiently compute the pairwise squared euclidean distance matrix in Matlab? Notation: Set one is given by a (numA,d)-matrix A and set two is ...
matheburg's user avatar
  • 2,120
12 votes
3 answers
25k views

OpenCV euclidean distance between two vectors

I want to calculate the euclidean distance between two vectors (or two Matrx rows, doesn't matter). Is there a good function for that in OpenCV?
farahm's user avatar
  • 1,306
10 votes
4 answers
56k views

How to calculate Euclidian distance between two points defined by matrix containing x, y?

I am very lost in Euclidean distance calculation. I have found functions dist2{SpatialTools} or rdist{fields} to do this, but they doesn´t work as expected. I suppose that one point has two ...
maycca's user avatar
  • 3,980
8 votes
3 answers
23k views

Python alternative for calculating pairwise distance between two sets of 2d points [duplicate]

In Matlab there exists the pdist2 command. Given the matrix mx2 and the matrix nx2, each row of matrices represents a 2d point. Now I want to create a mxn matrix such that (i,j) element represents the ...
user_1_1_1's user avatar
6 votes
2 answers
5k views

R - How to get row & column subscripts of matched elements from a distance matrix

I have an integer vector vec1 and I am generating a distant matrix using dist function. I want to get the coordinates (row and column) of element of certain value in the distance matrix. Essentially I ...
DKangeyan's user avatar
  • 533
6 votes
3 answers
5k views

Numpy: find the euclidean distance between two 3-D arrays

Given, two 3-D arrays of dimensions (2,2,2): A = [[[ 0, 0], [92, 92]], [[ 0, 92], [ 0, 92]]] B = [[[ 0, 0], [92, 0]], [[ 0, 92], [92, 92]]] How do you find the Euclidean ...
user1658296's user avatar
  • 1,418
5 votes
2 answers
4k views

Distance matrix from two separate data frames

I'd like to create a matrix which contains the euclidean distances of the rows from one data frame versus the rows from another. For example, say I have the following data frames: a <- c(1,2,3,4,5)...
h7681's user avatar
  • 355
4 votes
4 answers
1k views

(Speed Challenge) Any faster method to calculate distance matrix between rows of two matrices, in terms of Euclidean distance?

First of all, this is NOT the problem of calculating Euclidean distance between two matrices. Assuming I have two matrices x and y, e.g., set.seed(1) x <- matrix(rnorm(15), ncol=5) y <- matrix(...
ThomasIsCoding's user avatar
4 votes
2 answers
13k views

Optimize performance for calculation of euclidean distance between two images

I implemented the k-nearest-neighbours algorithm in python to classify some randomly picked images from the mnist database. However I found my distance function to be quite slow: An analisys of 10 ...
wodzu's user avatar
  • 3,112
3 votes
3 answers
12k views

Euclidean Distance

I have two points x1 = (a1,b1,c1,d1,e1); //5 dimensional point x2 = (a2,b2,c2,d2,e2); //5 dimensional point So is this right way to calculate the euclidean dist? d = sqrt(sqr(a1-a2)+sqr(...
Fox's user avatar
  • 9,424
3 votes
1 answer
657 views

Finding euclidean difference between coordinates in numpy

I am trying to calculate the difference between coordinates using numpy using following code : X = np.random.random((1000, 3)) # using broadcasting to calculate to find pairwise diffrence diff = X....
Mudits's user avatar
  • 1,573
3 votes
1 answer
947 views

How to use apply function to calculate the distance between two matrices

I'm trying to calculate the euclidean distance between two matrices. I have already achieved that using 2 for loops but trying to vectorize the calculation to speed up. I'm using pdist as a benchmark ...
Ray's user avatar
  • 59
3 votes
3 answers
3k views

Calculating Euclidean distance of pairs of 3D points in matlab

I have an Nx3 array that contains N 3D points a1 b1 c1 a2 b2 c2 .... aN bN cN I want to calculate Euclidean distance in a NxN array that measures the Euclidean distance between each pair of 3D ...
Mojtaba's user avatar
  • 33
3 votes
1 answer
784 views

Making an Euclidean Distance Matrix with Random Points in Python

I wrote a code that produces the desired number of points in a certain width and length range in the coordinate system. How can I calculate and tabulate the distance matrix of these points I produced ...
user avatar
2 votes
5 answers
3k views

Calculate Euclidean distances between all rows in matrices A and B

I have two matrices, A and B, with N_a and N_b rows, respectively. I need to calculate the Euclidean distance between all pairwise combinations of an element in A (a) and another in B (b), such that ...
Henry David Thorough's user avatar
2 votes
2 answers
227 views

Compute the difference matrix between a matrix and another matrix in MATLAB

Given a matrix A in matlab with: 3 1 4 5 7 8 and another matrix B which could be referred to as some reference points (each row is reference point that is to be compared to each row of A), 1 ...
Jesujoba Oluwadara ALABI's user avatar
2 votes
2 answers
707 views

Remove Elements of matrix by eucledian distance

I have an mxn matrix with E nonzero elements. The coordinates of the nonzeros are already available in an Ex2 vector. I'd like to check if the minimum Euclidean distance between every pair of ...
Mr Vinagi's user avatar
  • 410
2 votes
1 answer
2k views

Distance between two cells in a 2D matrix

I have a 2D matrix represented as a vector of values, an index representing the first cell and a pair of coordinate representing the second cell. vector<double> matrix; auto index = 10; auto ...
Nick's user avatar
  • 10.4k
2 votes
1 answer
895 views

Calculate distance matrix for 3D points

I have the lists xA, yA, zA and the lists xB, yB, zB in Matlab. The contain the the x,y and z coordinates of points of type A and type B. There may be different numbers of type A and type B points. I ...
EXIT_FAILURE's user avatar
2 votes
2 answers
1k views

Adjacency Matrix from Numpy array using Euclidean Distance

Can someone help me please on how to generate a weighted adjacency matrix from a numpy array based on euclidean distance between all rows, i.e 0 and 1, 0 and 2,.. 1 and 2,...? Given the following ...
Refka's user avatar
  • 55
2 votes
1 answer
1k views

How to find Euclidean Norm of rows of a matrix with BLAS?

I have an matrix M that I need to calculate its Euclidean Norm of its rows. Is it possible to use the efficiency of BLAS library for such purpose?
erogol's user avatar
  • 13.4k
2 votes
4 answers
9k views

How do I create a simliarity matrix in MATLAB?

I am working towards comparing multiple images. I have these image data as column vectors of a matrix called "images." I want to assess the similarity of images by first computing their Eucledian ...
Vivek Subramanian's user avatar
2 votes
2 answers
82 views

How to compute the Euclidean distance between two complex matrix by vectorization?

X=[x_1,x_2,...,x_N] is a [S,N] complex matrix. For example, S=3 and x_1=[1+2j,2+3j,3+4j]'. D is the distance matrix of X, which means D(i,j) is the Euclidean distance between x_i and x_j. my code: D = ...
Jie zhou's user avatar
2 votes
1 answer
341 views

Calculate distance between every point of matrix with each other

I want to calculate the euclidean distance between every element of an n-channeled matrix to every other and then apply the function exp(-dist) to it. My matrix is MxN so the output must be (MxN)x(MxN)...
Christina J's user avatar
1 vote
1 answer
4k views

R: pairwise Euclidean distance between columns of two matrices

The following loop takes too lonng to run (2mins/iteration) The tumor_signals is size 950000x422 The normal_signals is size 950000x772 Any ideas for how to speed it up? for(i in 1:ncol(tumor_signals))...
Cal Philic's user avatar
1 vote
4 answers
126 views

Speed up of the calculation of the sum the point-wise difference in R

Suppose I have two datasets. The first one is: t1<-sample(1:10,10,replace = T) t2<-sample(1:10,10,replace = T) t3<-sample(1:10,10,replace = T) t4<-sample(11:20,10,replace = T) t5<-...
Charlotte Deng's user avatar
1 vote
2 answers
138 views

Distances from a single point

I'm looking to generate a Euclidean distance matrix from one point [1,1]. This is what I have, however it doesn't work as intended: a=rand(10,10); a=sort(a); %sort example matrix it should be visible ...
user2305193's user avatar
  • 2,059
1 vote
1 answer
2k views

Euclidean distance between data sets in R using rdist from "fields" package

I am using rdist to compute Euclidean distances between a matrix and itself: > m = matrix(c(1,1,1,2,2,2,3,4,3),nrow=3, ncol=3) > m [,1] [,2] [,3] [1,] 1 2 3 [2,] 1 2 4 [3,...
Anne's user avatar
  • 6,902
1 vote
1 answer
1k views

A Pure Pythonic Pairwise Euclidean distance of rows of a numpy ndarray

I have a matrix of size (n_classes, n_features) and i want to compute the pairwise euclidean distance of each pair of classes so the output would be a (n_classes, n_classes) matrix where each cell has ...
Ash's user avatar
  • 3,480
1 vote
2 answers
2k views

MATLAB - efficient way of computing distances between points in a graph/network using the adjacency matrix and coordinates

I have the network representation in a 2D coordinate space. I have an adjacency matrix Adj(which is sparse) and a coordinate matrix with the x,y values of all the points/nodes/vertices in the graph ...
Vass's user avatar
  • 2,800
1 vote
2 answers
1k views

How to implement in Python a function to compute the Euclidean distance between two arbitrary points on a torus

Given a 10x10 grid (2d-array) filled randomly with numbers, either 0, 1 or 2. How can I find the Euclidean distance (the l2-norm of the distance vector) between two given points considering periodic ...
tanasr's user avatar
  • 115
1 vote
1 answer
530 views

Max Euclidean distance between two columns of a matrix in Matlab / Octave

I need to calculate for a program the maximal euclidian distance of vectors that are put together in a matrix A in Octave. What I have so far: for i=1:n for k=i:n+1 C=A(:,i)-A(:,k); ...
user avatar
1 vote
1 answer
5k views

Calculate Euclidean distance between RGB vectors in a large matrix

I have this RGB matrix of a set of different pixels. (N pixels => n rows, RGB => 3 columns). I have to calculate the minimum RGB distance between any two pixels from this matrix. I tried the loop ...
Tanatos Daniel's user avatar
1 vote
3 answers
697 views

how to compute the original vector from a distance matrix?

I have a small question about vector and matrix. Suppose a vector V = {v1, v2, ..., vn}. I generate a n-by-n distance matrix M defined as: M_ij = | v_i - v_j | such that i,j belong to [1, n]. That ...
sunjerry's user avatar
1 vote
1 answer
1k views

euclidean distance matrix

I'd like to calculate Euclidean distance between two words. First of all, each phoneme was vectorised: g = (0,1,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0) a = (0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,1,...
Hana Jee's user avatar
1 vote
1 answer
255 views

retrieve points based in Euclidean distance and axis values R

I have a dataframe with 11 variables represented by letters a to k, plotted in a bidimentional scatterplot. cor<-data.frame(X=c(0.36187115, -0.54755904, -0.82417308, -0.70806545, -0.77422866, -0....
Ricardo Adelino's user avatar
1 vote
1 answer
568 views

Vector Accelerated Euclidean Distance in 3D

I am in need of performing a very common and simple matrix operation. However I need it fast, really fast... I am already considering a multi-threaded implementation, however for now I just want to ...
Luigi Castelli's user avatar
1 vote
0 answers
25 views

Issue with designing loop in R for calculating Euclidean Distance Matrix and Mod Value

My objective is to calculate the determinant of the Euclidean Distance between each index and store the determinant value in a matrix. I have data as per the below format where columns B1, B2, ... B10 ...
Abhishek's user avatar
1 vote
0 answers
79 views

Bypass conversion of entire dist to matrix (Filter dist object)

I have a very big data frame that looks like this: > head(df) X Y Z 0:600000000 193.722 175.733 0.0998975 0:600000001 192.895 176.727 0.0998975 0:600000002 193.828 ...
Amaranta_Remedios's user avatar
1 vote
1 answer
161 views

distance between lat and long using a matrix

This is my data in a matrix. It is lat and long. I want the distance between point 1 and 2, 2 and 3, 3 and 4, etc. > mat<-as.matrix(b) > mat buoy.LON buoy.LAT [1,] -...
R.Gould's user avatar
  • 11
1 vote
1 answer
105 views

How to manipulate matrix addition and multiplication for Euclidean distance computation?

I have the following: A = [1 2 3; 4 5 6; 7 8 9]; B = [10 11 12; 13 14 15]; [N1, D1] = size(A); [N2, D2] = size(B); A_sq = sum(A.^2, 2); B_sq = sum(B.^2, 2)'; D = A_sq(:,ones(1,N2)) + B_sq(ones(1,...
Hello World's user avatar
0 votes
2 answers
233 views

Eucledian distance matrix between two matrices

I have the following function that calculates the eucledian distance between all combinations of the vectors in Matrix A and Matrix B def distance_matrix(A,B): n=A.shape[1] m=B.shape[1] C=...
jan-seins's user avatar
  • 1,273
0 votes
1 answer
234 views

How to incorporate elevation into euclidean distance matrix in pandas?

I have the following dataframe in pandas: import pandas as pd df = pd.DataFrame({ "CityId": { "0": 0, "1": 1, "2": 2, "3": 3, "4": 4 }, "X": {...
ZeroStack's user avatar
  • 1,069
0 votes
1 answer
160 views

How to go from a vector to a similarity matrix?

I would like to reconstruct a similarity matrix between two vectors from a vector containing the similarity between each pair of elements in the two vectors. Does anyone know how I could do it? To ...
ben's user avatar
  • 277
0 votes
3 answers
2k views

How to compute euclidean distance between all column vector pairs for a given matrix without using loops? (using only numpy) [duplicate]

As titled, I need to calculate the euclidean distance between all possible column vector pairs of a given matrix without using loops and using numpy only. This produces the output I'm looking for (...
JoeAverage's user avatar
0 votes
1 answer
481 views

Is there a more efficient way to generate a distance matrix in numpy

I was wondering if there is a more straight forward, more efficient way of generating a distance matrix given the H x W of the matrix, and the starting index location. For simplicity lets take a 3x3 ...
user1179317's user avatar
  • 2,803
0 votes
2 answers
2k views

Python Numpy efficient Polar euclidean distance

I have a list of n polar coordinates, and a distance function which takes in two coordinates. I want to create an n x n matrix which contains the pairwise distances under my function. I realize I ...
Nezo's user avatar
  • 577
0 votes
1 answer
1k views

Is it possible to calculate Euclidean Distance between two varying length matrices?

I have started working on online signature data-set for verification purpose. I have two matrices containing digitized data of two signatures of varying length (the number of rows differ). e.g. one is ...
Jawad Chughtai's user avatar