Skip to content

Instantly share code, notes, and snippets.

@markuskreitzer
Created September 5, 2017 19:52
Show Gist options
  • Save markuskreitzer/8c3797b4504f51d3778790a8dc4a326f to your computer and use it in GitHub Desktop.
Save markuskreitzer/8c3797b4504f51d3778790a8dc4a326f to your computer and use it in GitHub Desktop.
Prints distance between all points in a 2x2 matrix
#!/usr/bin/env python
import numpy as np
a = np.ones([2,2])
for from_rownum, row in enumerate(a):
for from_colnum, number in enumerate(row):
print "Cell:", from_rownum, from_colnum
for to_rownum, row in enumerate(a):
for to_colnum, number in enumerate(row):
distance = np.sqrt((to_rownum - from_rownum)**2 + (to_colnum - from_colnum)**2)
print " to:", to_rownum, to_colnum, "distance:", distance
./test.py
Cell: 0 0
to: 0 0 distance: 0.0
to: 0 1 distance: 1.0
to: 1 0 distance: 1.0
to: 1 1 distance: 1.41421356237
Cell: 0 1
to: 0 0 distance: 1.0
to: 0 1 distance: 0.0
to: 1 0 distance: 1.41421356237
to: 1 1 distance: 1.0
Cell: 1 0
to: 0 0 distance: 1.0
to: 0 1 distance: 1.41421356237
to: 1 0 distance: 0.0
to: 1 1 distance: 1.0
Cell: 1 1
to: 0 0 distance: 1.41421356237
to: 0 1 distance: 1.0
to: 1 0 distance: 1.0
to: 1 1 distance: 0.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment