Checking elements in a matrix in python

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]])
+4
source share
4 answers

, ndarray,

Matrix[a]

- 1- 2D. ,

Matrix[a][b]

( IndexError, Matrix[a] ).

Matrix[a, b]

. , . ,

Matrix == value

, astype, . , . , dtype int32, , ,

return (Matrix == value).astype(numpy.int32)

, numpy.equal ufunc out:

numpy.equal(Matrix, value, out=Matrix)
+5

np.where .

:

>>> matrix
array([[0, 1, 1, 2],
       [1, 0, 2, 1],
       [1, 2, 0, 1],
       [2, 1, 1, 0]])

2 matrix 0 :

>>> np.where(matrix==2,0,matrix)
array([[0, 1, 1, 0],
       [1, 0, 0, 1],
       [1, 0, 0, 1],
       [0, 1, 1, 0]])

2 0 1:

>>> np.where(matrix==2,0,1)
array([[1, 1, 1, 0],
       [1, 1, 0, 1],
       [1, 0, 1, 1],
       [0, 1, 1, 1]])

:

>>> np.where(matrix==2,'  a two','not two')
array([['not two', 'not two', 'not two', '  a two'],
       ['not two', 'not two', '  a two', 'not two'],
       ['not two', '  a two', 'not two', 'not two'],
       ['  a two', 'not two', 'not two', 'not two']], 
      dtype='<U7')
+1

, . value == 2, , , . , 2.0 2.0, 1.999999999 - ?

( IPython-)

In [35]: A = array([1.999999999, 1.999999999])

In [36]: A
Out[36]: array([ 2.,  2.])

In [37]: A == 2
Out[37]: array([False, False], dtype=bool)

, , A , "2.0", , , .

:

, numpy.isclose

ok_mask = np.isclose(Matrix, value)
fail_mask = ~ok_mask
Matrix[ok_mask] = 1
Matrix[fail_mask] = 0

, , , , .

+1
source

I am not familiar with scipy, but if it Matrixis a regular list, I would do the following:

#Assuming l is the length of Matrix
for a in range(l):
      for b in range(len(Matrix[a])):
               if Matrix[a][b] == value:
                    Matrix[a][b] = 1
               else:
                    Matrix[a][b] = 0

Here is a small demo:

>>> Matrix = [[1,2], [3,4]]
>>> value = 2
>>> l = len(Matrix)
>>> for a in range(l):
      for b in range(len(Matrix[a])):
               if Matrix[a][b] == value:
                    Matrix[a][b] = 1
               else:
                    Matrix[a][b] = 0


>>> Matrix
[[0, 1], [0, 0]]
0
source

All Articles