Problem attaching value to Python 2 matrix

Asked

Viewed 96 times

1

The problem is in matrix.append([(x+1), vectorY[x][1]]).
No matter how much vextor[x][1] is different from (x+1), when added you will receive the same value as (x+1), leaving a matrix with two equal values, ex: ([1 , 1])
This is the full code link: https://gist.github.com/anonymous/e6e89ee4b4dcc6e6b247#file-spearman_eng-py

def createMatrixDi(self, matrixxy):
    matrixxy.sort()
    vectorY = []
    index = 1
    for values in matrixxy: 
        vectorY.append( [ values[1], index ])       
        index += 1
    x = 0
    matrix= []
    while (x < len(vectorY)):
        matrix.append( [(x+1), vectorY[x][1]] )
        x += 1
    return(matrix, len(vectorY))
  • Which error?

  • The error is that the matrix is being generated wrong. Instead of creating a matrix of the type: ([1,2], [2,4],[3,3]) , a matrix of always equal values is being created: ([1,1], [2,2], [3,3])

  • And what input is used? The value of matrixxy?

  • No matter the input, it will always generate this error. This error is inexplicable, I’m already thinking it is Python bug. I’ll use numpy and if it works I put the solution here.

1 answer

1

I simulated the execution of your code sequentially, in parts. I adapted the code a little and did the following:

a = [[6, 2], [8, 4], [1, 9]]
a.sort()

print(a)

vectorY = []
index = 1

for values in a: 
    vectorY.append( [ values[1], index ])       
    index += 1

print(vectorY)

a printed:

[[1, 9], [6, 2], [8, 4]]

vectorY printed:

[[9, 1], [2, 2], [4, 3]]

Then we have:

x = 0
matrix = []

while (x < len(vectorY)):
    matrix.append( [(x+1), vectorY[x][1]] )
    x += 1

print(matrix, len(vectorY))

Who printed:

([[1, 1], [2, 2], [3, 3]], 3)

There is no problem with your code. In fact, the unwanted behavior is on this line:

matrix.append( [(x+1), vectorY[x][1]] )

But notice that vectorY[x][1], whatever the x, will take the second value of [[9, 1], [2, 2], [4, 3]], that in the previous loop was already sequential.

If I understand what you want, the expected result is:

([[1, 9], [2, 2], [3, 4]], 3)

That is to say:

matrix.append( [(x+1), vectorY[x][0]] )
  • 1

    Thanks Mendez! The 'vectorY[x][1]' really is sequential and so the error.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.