1

I have two variables containing an array of 300-dimensional vectors called a and b. Of which the minimal value is -10.0 and the max value is 10.0 per vector value.

My goal is to visualize the distance in a two-dimensional space.

The Problem

Visualizing in one dimension is relatively easy. I take the vectors of a and vectors of b, calculate the Euclidean distance and I can visualize over one dimension. But I now want to visualize over an x-axis and an y-axis.

Pseudo Python

a = [0.1, 0.343423, -2.9008, etc...]
b = [-0.3455, -6.03983, 9.098, etc...]
distance = calculateEuclidean(a, b)
print(distance) # 6.39878 | this is 1 dimension... How to make it 2?

Examples in any programming language are welcome :-)

PS:
My question is not about how to calculate the Euclidean distance (Stackoverflow is full of it) but rather how to express it in two rather than one dimension.

Bob van Luijt
  • 7,153
  • 12
  • 58
  • 101
  • 2
    By definition, wouldn't you be calculating a scalar? So it can't be 2d. – Colin Ricardo Dec 11 '18 at 11:40
  • Yes, that is true. No way to visualize (dare I say: hack) a scalar in a 2-dimensional space? – Bob van Luijt Dec 11 '18 at 11:41
  • 1
    Not sure what you are asking here but how about using the components of the displacement vector between the two points? Distance is usually just taken as the modulus of the displacement vector and hence a scalar so not really sure what you are looking for? – mathematician1975 Dec 11 '18 at 11:42
  • Can you post an example of a vector? I suppose it should be `[x1, x2, y1, y2]`. https://stackoverflow.com/questions/42281966/how-to-plot-vectors-in-python-using-matplotlib – iGian Dec 11 '18 at 11:43
  • I've updated the question to be a bit more precise. – Bob van Luijt Dec 11 '18 at 11:45
  • 1
    The question really isn't clear. Distance is a single number. If for some reason you want to make a 2-d plot out of a set of distances, perhaps you could use the second coordinate for a different, non-euclidean distance (such as the sup-metric) – John Coleman Dec 11 '18 at 11:55
  • I'm voting to close this as off-topic here. It might be a good question for [datascience.se]. It seems like it might be a good fit for their `visualization` or `data-visualization` tag. – John Coleman Dec 11 '18 at 12:25
  • Distance is a scalar. If you have 2D vectors when you start, you are free to plot those in 2D space. Do you mean showing the distribution of x- and y- differences between your A and B resultant vector? An understanding of Euclidean vectors will go a long way. – duffymo Dec 11 '18 at 15:38

2 Answers2

0

Once the eucludia distance is give by

Formula here

The result is always a scallar. If you want calculate for individual dimensions you should use

a = [1,2,3]
b = [4,5,6]
distance1 = euclidianDistance(a[0],b[0]) //first dimension
distance2 = euclidianDistance(a[1],b[1]) //second dimension
distance3 = euclidianDistance(a[0],b[0]) //third dimension
  • That makes sense, but now I only have the first two values of the array and I'll be missing the remaining 298 :) – Bob van Luijt Dec 11 '18 at 11:47
  • Sorry, it's just an example. you couls add them on a list called distances like: for i in range (0,3): distances.append(euclidianDistance(a[i],b[i])) – Daladier Sampaio Dec 11 '18 at 11:49
0

You can easily do in R language by expressing 2 vectors as matrix.

B = matrix(c=(2,3,4,3,8,2)
       nrow=3,
       ncol=2)
B gives you
 [,1] [,2] 
 [1,]    2    3 
 [2,]    4    3 
 [3,]    8    2

 v1 = (2,4,8)
 v2 = (3,3,2)

You can work with matrix standard operations like transposing. Go deep into documentation.

Euclidian distance

rdist is from the package fields

rdist(x1, x2)

Given two matrixes it computes distance.

p stands for Minkowski power.

For Euclidian use euclidean.

rdist(X, metric = "euclidean", p = 2L)

"euclidean": sqrt(sum_i((v_i - w_i)^2))

"minkowski": (sum_i(|v_i - w_i|^p))^{1/p}

Laci R
  • 31
  • 1
  • 7
  • 1) The question isn't how to *compute* Euclidean distance (which is rather trivial in any programming language) and 2) `rdist()` isn't base R. You should mention which package you are using. – John Coleman Dec 11 '18 at 11:59