1

I have a vector with int values like:

v=[10,8,6]

and what I want is to create an m*m matrix that stores the distance between these elements, i.e. take each element of the vector and substract it from all the other ones, so at the end I will end up with:

 m[3][3]=10-10   10-8   10-6
         8-10    8-8    8-6
         6-10    6-8    6-6

I want to implement it into Python, but without using NumPy. I have done this so far:

def main():
    v=[10,8,6]
    l=len(v)
    m=[]
    #filling the matrix
    for i in range(0,l-1):
        for j in range(0,l-1):
            m[i][j]=abs(v[i]-v[j])

    #visualize the matrix
    for i in range(0,l-1):
        for j in range(0,l-1):
            print m[i][j]

But I am getting some error that does not recognize with the bounds of m. Why is that?

4 Answers 4

4
v= [10,8,6] 
m = [[abs(y-x) for y in v] for x in v]

EDIT:

For pretty printing you can use something like:

for i in m:
 print '%s '*len(i) % tuple(i)
Sign up to request clarification or add additional context in comments.

3 Comments

One of these days, I'll be good enough at Python to rattle off two or more list comprehensions at once. (+1)
it seems nice solution, but I need to store those results in one matrix so I can used it later
@Manolo Not sure that I understood your comment. This code does the same things as the one from your question suppose to do, but in shorter way.
1

You need to make a list for each row inside of the enveloping list. I used a double list comprehension to make m and then made the formatting a little prettier for printing out the matrix. Also, watch your indices: remember that range goes from the first index to one minus the second index passed to it. You could also do print ' '.join(row) for each row in m to print it out nicely.

def main():
    v=[10,8,6]
    l=len(v)
    #filling the matrix
    m=[[abs(x - y) for y in v] for x in v]

    #visualize the matrix
    for i in range(0,l):
        for j in range(0,l):
            print m[i][j],
        print
main()

result:

0 2 4
2 0 2
4 2 0

Comments

1

Your list starts out empty. You can't index the element in the list. One way to solve this would be to create the lists and then append them.

m=[]
#filling the matrix
for i in range(0,l-1):
    x = []
    for j in range(0,l-1):
        x.append( abs(v[i]-v[j]) )
    m.append(x)

Or you could create the matrix and then fill it up

m=[[0] *l for x in range(l)]
#filling the matrix
for i in range(0,l-1):
    for j in range(0,l-1):
        m[i][j]=abs(v[i]-v[j])

better yet is the list comprehension other have shown

m = [[abs(y-x) for y in v] for x in v]

but then, I'd use numpy/scipy

m = scipy.spatial.distance.pdist(v)

Comments

0

You need to initialize each object in the first dimension as an array, otherwise it's like you're basically trying to index on None.

#filling the matrix
for i in range(0,l-1):
    m[i] = []
    for j in range(0,l-1):
        m[i][j]=abs(v[i]-v[j])

1 Comment

Your code is incorrect. m[i][j] will be an invalid index into the zero length list m[i] = []

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.