1

I currently have a matrix like this:

[[2, 6, 8, 9, 8, 6, 3], [4, 8, 10, 10, 7, 5], [5, 6, 8, 8, 9], [4, 6, 7, 8], [3, 5, 9], [3, 6], [4]]

such that the first array is distances from city 1 to 2,3,4,5,6,7,8 the second is distances from city 2 to 3,4,5,6,7,8 ... the 7th array is the distance from city 7 to city 8. I need to convert it into a proper distance matrix.

So far, I've inserted 0's to make it size 7 and it becomes:

[[2, 6, 8, 9, 8, 6, 3], [0, 4, 8, 10, 10, 7, 5], [0, 0, 5, 6, 8, 8, 9], [0, 0, 0, 4, 6, 7, 8], [0, 0, 0, 0, 3, 5, 9], [0, 0, 0, 0, 0, 3, 6], [0, 0, 0, 0, 0, 0, 4]]

I then did

for i,j if distance==0 then distances[i][j]=distances[j][i] 

and it becomes:

[[2, 6, 8, 9, 8, 6, 3], [6, 4, 8, 10, 10, 7, 5], [8, 8, 5, 6, 8, 8, 9], [9, 10, 6, 4, 6, 7, 8], [8, 10, 8, 6, 3, 5, 9], [6, 7, 8, 7, 5, 3, 6], [3, 5, 9, 8, 9, 6, 4]]

I then inserted 0's to include the distance from city to same city:

[[0, 2, 6, 8, 9, 8, 6, 3], [6, 0, 4, 8, 10, 10, 7, 5], [8, 8, 0, 5, 6, 8, 8, 9], [9, 10, 6, 0, 4, 6, 7, 8], [8, 10, 8, 6, 0, 3, 5, 9], [6, 7, 8, 7, 5, 0, 3, 6], [3, 5, 9, 8, 9, 6, 0, 4]]

and finally added on the final city's distances:

[[0, 2, 6, 8, 9, 8, 6, 3], [6, 0, 4, 8, 10, 10, 7, 5], [8, 8, 0, 5, 6, 8, 8, 9], [9, 10, 6, 0, 4, 6, 7, 8], [8, 10, 8, 6, 0, 3, 5, 9], [6, 7, 8, 7, 5, 0, 3, 6], [3, 5, 9, 8, 9, 6, 0, 4], [3, 5, 9, 8, 9, 6, 4, 0]]

but I appear to have gone wrong somewhere, as I have not produced the correct distance matrix. I believe it may be the distances[i][j]=[j][i] bit, but I'm not entirely sure.

Sid Jones
  • 55
  • 1
  • 1
  • 10

3 Answers3

2

This is actually quite a common-form and if you are able to use python's excellent scientific-stack (numpy and scipy needed for this approach), this is already available:

import numpy as np
from scipy.spatial.distance import squareform

raw = np.array([[2, 6, 8, 9, 8, 6, 3], [4, 8, 10, 10, 7, 5], [5, 6, 8, 8, 9],
    [4, 6, 7, 8], [3, 5, 9], [3, 6], [4]])

print(squareform(np.hstack(raw)))  # use of hstack as some kind flattening here
                                   # many alternatives possible

Output:

[[ 0  2  6  8  9  8  6  3]
 [ 2  0  4  8 10 10  7  5]
 [ 6  4  0  5  6  8  8  9]
 [ 8  8  5  0  4  6  7  8]
 [ 9 10  6  4  0  3  5  9]
 [ 8 10  8  6  3  0  3  6]
 [ 6  7  8  7  5  3  0  4]
 [ 3  5  9  8  9  6  4  0]]

Of course, this function works both ways.

sascha
  • 32,238
  • 6
  • 68
  • 110
1

Here is another way to approach the problem without requiring imports.

data = [[2, 6, 8, 9, 8, 6, 3], [4, 8, 10, 10, 7, 5], [5, 6, 8, 8, 9], [4, 6, 7, 8], [3, 5, 9], [3, 6], [4]]
data.append([])
for city in range(len(data)):
    for missing in range(city):
        data[city].insert(missing, data[missing][city])
    data[city].insert(city, 0)

data will then contain:

[[0, 2,  6, 8, 9,  8,  6, 3],
 [2, 0,  4, 8, 10, 10, 7, 5],
 [6, 4,  0, 5, 6,  8,  8, 9],
 [8, 8,  5, 0, 4,  6,  7, 8],
 [9, 10, 6, 4, 0,  3,  5, 9],
 [8, 10, 8, 6, 3,  0,  3, 6],
 [6, 7,  8, 7, 5,  3,  0, 4],
 [3, 5,  9, 8, 9,  6,  4, 0]]
Ryan
  • 2,073
  • 1
  • 19
  • 33
0

I think this works:

d = [[2, 6, 8, 9, 8, 6, 3], [4, 8, 10, 10, 7, 5], [5, 6, 8, 8, 9], [4, 6, 7, 8], [3, 5, 9], [3, 6], [4]]

matrix = [0] * 8
for i in range(8):
    matrix[i] = [0] * 8

j = 0
for element in d:
    i = j
    for value in element:
        matrix[i+1][j] = value
        matrix[j][i+1] = value
        i += 1
    j += 1
Manuel
  • 698
  • 4
  • 8