I have a matrix in scipy. And I'm trying to replace it with 1 if it meets a certain condition, and 0 if it is not.
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 matter in it. However, it gives me the result as a matrix that is completely 0.
This worked before on a similar script. Perhaps this is something with a matrix structure?
Here's what the matrix looks like at the beginning -
[ [0 1. 1. 2.]
[1. 0. 2. 1.]
[1. 2. 0. 1.]
[2. 1. 1. 0.]]
When I set value == 1. I get all 1 to 1, and all 2 to zero. This is what I want.
But, when I set value == 2. I get everything to zero.
when I do everything that 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]])