euclidean-distance

Efficiently compute pairwise squared Euclidean distance in Matlab

若如初见. 提交于 2019-12-16 20:15:44
问题 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 given by a (numB,d) -matrix B . The resulting distance matrix shall be of the format (numA,numB) . Example points: d = 4; % dimension numA = 100; % number of set 1 points numB = 200; % number of set 2 points A = rand(numA,d); % set 1 given as matrix A B = rand(numB,d); % set 2 given as matrix B

Euclidian Distances between points

笑着哭i 提交于 2019-12-13 14:43:05
问题 I have an array of points in numpy: points = rand(dim, n_points) And I want to: Calculate all the l2 norm (euclidian distance) between a certain point and all other points Calculate all pairwise distances. and preferably all numpy and no for's. How can one do it? 回答1: If you're willing to use SciPy, the scipy.spatial.distance module (the functions cdist and/or pdist ) do exactly what you want, with all the looping done in C. You can do it with broadcasting too but there's some extra memory

K-Nearest Neighbor Implementation for Strings (Unstructured data) in Java

孤者浪人 提交于 2019-12-13 07:53:37
问题 I'm looking for implementation for K-Nearest Neighbor algorithm in Java for unstructured data. I found many implementation for numeric data, however how I can implement it and calculate the Euclidean Distance for text (Strings). Here is one example for double: public static double EuclideanDistance(double [] X, double []Y) { int count = 0; double distance = 0.0; double sum = 0.0; if(X.length != Y.length) { try { throw new Exception("the number of elements" + " in X must match the number of

Code to generate 3D random geometric graph in r

牧云@^-^@ 提交于 2019-12-12 03:09:54
问题 Hi all i want to generate 3D geometric random graph in r. In igraph there is no function of generating the graph in 3D. In 3D geometric random graph i found the euclidean distance between of the uniformly distributed points in 3D space and considered an edge if the distance is less than or equal to some threshold. Tried the following. ##Code to generate random geometric graph in 3D ##To generate geometric random graph in 3D I have placed points in 3D space in uniform distribution the number

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

喜你入骨 提交于 2019-12-11 16:33:10
问题 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 matrix where the starting point is (0,0). Thus, the distance matrix to be generated is: [[ 0. 1. 2. ] [ 1. 1.41421356 2.23606798] [ 2. 2.23606798 2.82842712]] Index (0,1) is 1 distance away, while index (2,2) is 2.828 distance away. The code I have so far is below: def get_distances(start, height,

Efficient way to compute multiple euclidean distances Matlab

喜你入骨 提交于 2019-12-11 13:45:35
问题 I am training my own self organizing map to cluster colorvalues. Now I want to make some sort of a U-matrix to show euclidean distances between the nodes and their direct neighbors. My problem now is, that my algorithm is quite unefficient!! There is certainly a way to compute this more efficiently? function displayUmatrix(dims,weights) %#dims is [30 30], size(weights) = [900 3], %#consisting of values between 1 and 0 hold on; axis off; A = zeros(dims(1), dims(2), 3); B = reshape(weights',

Calculate Euclidean distance between RGB vectors in a large matrix

风格不统一 提交于 2019-12-11 11:37:31
问题 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 approach, but because the set is too big (let's say N=24000), it looks like it will take forever for the program to finish. Is there another approach? I read about pdist , but the RGB Euclidean distance cannot be used with it. k=1; for i = 1:N for j = 1:N if (i~=j) dist_vect(k)=RGB_dist(U(i,1),U(j,1)

Find distance between two points using MKMapKit

一笑奈何 提交于 2019-12-11 06:46:48
问题 I'm attempting to find the euclidean distance in meters between two points on an MKMapView using iPhone OS 3.2. The problem is that I have these coordinates in terms of latitude and longitude, which, mathematically provides me enough data to find the distance, but it's going to take some tricky trigonometry. Is there any simpler solution? Thanks! 回答1: CLLocation has a method to calculate the distance for you: - (CLLocationDistance)distanceFromLocation:(const CLLocation *)location API docs

Apache Mahout + Euclidean Distance: Unexpected Results

醉酒当歌 提交于 2019-12-11 05:59:02
问题 I'm using Mahout's EuclideanDistanceSimilarity class to rank the similarity of several users given the following data set of user preferences. The range for preferences is currently all integers from 1 to 5 inclusive. However I have control over the scale, so that can change if it would help. User Preferences: Item 1 Item 2 Item 3 Item 4 Item 5 Item 6 1 2 4 3 5 1 2 2 5 1 5 1 5 1 3 1 5 1 5 1 5 4 2 4 3 5 1 2 5 3 3 4 5 2 2 I'm getting unexpected results when I run the following test code, which

Calculate the euclidian distance between an array of points to a line segment in Python without for loop

守給你的承諾、 提交于 2019-12-11 05:35:25
问题 I'm looking for a function to compute the euclidian distance between a numpy array of points with two coordinates (x, y) and a line segment. My goal is to have a result in under 0.01 sec for a line segment and 10k points. I already found a function for a single point. But running a for loop is very inefficient. I also found this function that calculates the distance to the infinite line: def line_dists(points, start, end): if np.all(start == end): return np.linalg.norm(points - start, axis=1)