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
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
9 votes
6 answers
22k views

Fastest way to calculate Euclidean distance in c

I need to calculate euclidean distance between two points in the fastest way possible. In C. My code is this and seems a little bit slow: float distance(int py, int px, int jy, int jx){ return ...
Pol's user avatar
  • 103
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
3 votes
2 answers
5k views

Finding squared distances beteen n points to m points in numpy

I have 2 numpy arrays (say X and Y) which each row represents a point vector. I would like to find the squared euclidean distances (will call this 'dist') between each point in X to each point in Y. I ...
member555's user avatar
  • 797
3 votes
1 answer
3k views

Calculate euclidean distance in a faster way

I want to calculate the euclidean distances between rows of a dataframe with 30.000 observations. A simple way to do this is the dist function (e.g., dist(data)). However, since my dataframe is large, ...
Joachim Schork's user avatar
2 votes
2 answers
344 views

How to improve processing time for euclidean distance calculation

I'm trying to calculate the weighted euclidean distance (squared) between twoo data frames that have the same number of columns (variables) and different number of rows (observations). The ...
Ana F.'s user avatar
  • 43
2 votes
1 answer
4k views

Numpy operation for euclidean distance between multidimensional arrays

I have two numpy arrays. 'A' of size w,h,2 and 'B' with n,2. In other words, A is a 2-dimensional array of 2D vectors while B is a 1D array of 2D vectors. What i want as a result is an array of size w,...
Mario Dekena's user avatar
2 votes
1 answer
200 views

For loop on two arrays of points

I have two arrays of points: list1 with list1.shape = [N, 3] and list2 with list2.shape = [M, 3]. Within N, M: the total number of point, (x, y, z) are 3 coordinates in 3D. Now I want to check if ...
user2863620's user avatar
2 votes
0 answers
663 views

Minimum euclidean distances and corresponding indices between array and array columns

UPDATE Just in case this is useful for anyone. As stated below, the Euclidean distance calculation and np.argmin accounted for almost all of the runtime. By rewriting the distance calculation with ...
arrow1's user avatar
  • 21
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
1 answer
1k views

Calculating euclidean distances with Python runs too slow

I read to datasets from file into numpy arrays like this: def read_data(filename): data = np.empty(shape=[0, 65], dtype=int) with open(filename) as f: for line in f: data = np....
Fogarasi Norbert's user avatar
1 vote
1 answer
77 views

High performance computation of mean 2D-Euclidian-distance

Let's say I have a position matrix P with dimensions 10x2, where the first column contains x values and second column the corresponding y values. I want the mean of the lengths of the positions. The ...
Elias S.'s user avatar
  • 231
1 vote
1 answer
97 views

Apply PHP compare function to all elements in MySQL table

I'm building an algorithm that takes an euclidean coordinate—for example (-4, 2)—and searches through a large database table of other coordinates to find the closest coordinates (using euclidean ...
Leopold Joy's user avatar
  • 4,600
1 vote
3 answers
4k views

Minimum, mean and maximum distance between points 3-D in Python

I have a list of x,y,z points. Using the formula to find the distance between two points in 3-D import math import numpy as np point0 = x0, y0, z0 point1 = x1, y1, z1 dist = math.sqrt((x0-x1)**...
Gianni Spear's user avatar
  • 7,406
1 vote
0 answers
47 views

Why broadcasting is slower than for loop in this case?

I wrote a simple code to compare the performance of broadcasting vs for loop. The code is as below: import numpy as np D = 3072 num_train = 5000 test = np.random.rand(D) X_train = np.random.rand(...
Changda Li's user avatar
1 vote
1 answer
484 views

find the most efficient path (in term of shortest distance) between a set of 2D points

I have a set of 2D points stored in a dictionary and i need to find the most efficient path to sampling all points (red traingles) in term of the shortest distance from a start-end point (yellow ...
Gianni Spear's user avatar
  • 7,406
1 vote
0 answers
57 views

Find closest point to another point with NumPy and/or SciPy quickly [duplicate]

I want to know the fastest way in python to calculate the closest point in a list of points to my point. There is already this question, but it does not consider performance and there are the ...
noɥʇʎԀʎzɐɹƆ's user avatar
0 votes
2 answers
2k views

Efficient euclidean distance calculation in python for millions of rows

I am trying to find the euclidean distance between elements of two data sets. Each has millions of elements. After calculating euclidean distance, I need the closest match. Given the number of ...
shreya jain's user avatar
0 votes
3 answers
365 views

Finding nearest pixel in defined color space - quick implementation using numpy

I have been working on a task, where I implemented median cut for image quantization – representing the whole image by only limited set of pixels. I implemented the algorithm and now I am trying to ...
Toby V.'s user avatar
  • 455
0 votes
2 answers
1k views

Calculating Euclidean distance with a lot of pairs of points is too slow in Python

The main goal is to generate the customer similarity based on Euclidean distance, and find the 5 most similar customers for each customer. I have 400,000 customers data, each of them has 40 attributes....
ZhaiShang's user avatar
  • 123
0 votes
2 answers
170 views

Python: Improve the speed of Euclidean distance calculation in a class

I have a class component that calculates the Euclidean distance between the last elements in arrays within 2 dictionaries. One dictionary contents the tracking trajectory of blobs (r), and the other ...
programandoconro's user avatar
0 votes
1 answer
53 views

Speed-up special case euclidean distance in MATLAB

I want to optimize the following code example: %Example definition A = rand(40,10000,250); B = rand(40,10000,250); %Euclidean seuc = sum((A-B).^2, 3); Do you have any idea how to speed this up? Or ...
yoshiii90's user avatar
-1 votes
3 answers
2k views

how to define a pow2 macro in C++

I want to have definition for pow2C as follows. But my compiler shows it (__tmpDouble__) is already defined. Please help me define it. #ifndef pow2C double __tmpDouble__; #define pow2C(a) ((...
remo's user avatar
  • 888
-2 votes
2 answers
226 views

How can I speed up my 3D Euclidean distance matrix code [closed]

I have created code to calculate the distance of all objects (tagID) from one another based on x, y, z coordinates (TX, TY, TZ) at each time step (Frame). While this code does work, it is too slow for ...
VacciniumC's user avatar