# Given a list of n points, how can I generate a matrix that contains the distance from each point to each other point using numpy?

Hey guys so i'm trying to rewrite the following matlab code in python:

``````repmat(points, 1, length(points)) - repmat(points', length(points),1);
```
```

`points` is an array that contain the radian value of several points.

The above code gives me a matrix output like this:

`````` 0   1   2   0   1   2   0   1   2
-1   0   1  -1   0   1  -1   0   1
-2  -1   0  -2  -1   0  -2  -1   0
0   1   2   0   1   2   0   1   2
-1   0   1  -1   0   1  -1   0   1
-2  -1   0  -2  -1   0  -2  -1   0
0   1   2   0   1   2   0   1   2
-1   0   1  -1   0   1  -1   0   1
-2  -1   0  -2  -1   0  -2  -1   0
```
```

Which i can easily manipulate to get the distance from each point to every other point.

I was just wondering if there's a one liner way to do it using numpy?

I tried the following which didn't work:

``````np.tile(points, (1, len(points))) -
np.tile(points.T, (len(points), 1))
```
```

Anyone has any ideas?

In MATLAB, you had to use `repmat` because you needed the arrays on the left and right side of the `-` to be the same size. With numpy, this does not have to be the case thanks to numpy's automatic broadcasting. Instead, you can simply subtract one from the other and the automatic broadcasting will create your result of the expected size.

``````# Create some example data
points = np.array([[1,2,3,4]]);

# Just subtract the transpose from points
B = points - points.T

#   array([[ 0,  1,  2,  3],
#          [-1,  0,  1,  2],
#          [-2, -1,  0,  1],
#          [-3, -2, -1,  0]])
```
```

If `points` is instead just a 1D array, then @JoshAdel's answer should work for you (which also uses broadcasting) or you could convert it to a 2D array.