I have a matrix in scipy. And I'm trying to replace it with a 1 if it meets a certain condition, and a 0 if it doesnt.
for a in range(0,l):
for b in range(0,l):
if Matrix[a][b] == value:
Matrix[a][b] = 1
else:
Matrix[a][b] = 0
My matrix is full of elements that have the "value" in it. Yet it's giving me the output as a matrix that is entirely 0's.
This worked before on a similar script. Is it perhaps something to with the structure of the matrix?
Here's how the matrix looks at first--
[ [0 1. 1. 2.]
[1. 0. 2. 1.]
[1. 2. 0. 1.]
[2. 1. 1. 0.]]
When i set value == 1. I get all the 1's to 1's, and all the 2's to zero. Which is what I want.
But, when i set value == 2. I get everything to zero.
when I do all of what has been suggested.
[[ 0. 1. 1. 2. 1. 2. 2. 3.]
[ 1. 0. 2. 1. 2. 1. 3. 2.]
[ 1. 2. 0. 1. 2. 3. 1. 2.]
[ 2. 1. 1. 0. 3. 2. 2. 1.]
[ 1. 2. 2. 3. 0. 1. 1. 2.]
[ 2. 1. 3. 2. 1. 0. 2. 1.]
[ 2. 3. 1. 2. 1. 2. 0. 1.]
[ 3. 2. 2. 1. 2. 1. 1. 0.]]
>> np.where(matrix==2,1,0)
>> array([[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0]])
l?print(a,b,repr(Matrix[a][b]), type(Matrix[a][b]), repr(value), type(value)). If the matrix is numerical, you can also addprint(Matrix[a][b]-value). You'll either see a bunch of output which will be useful, or you might see nothing at all, which would tell you something else.import pdb;pdb.set_trace()which starts the debugger at wherever you insert it. Then in the debugger you can runprint a,bto view the variables andprint Matrix[a][b]to find out the value of that element of the matrix. You can use next to progress to the next line of the code. That way you can find out what the code is actually doing. For more information on the commands use the help command.