Python matrix: need to show where values are equal to vector
Question:
I have this vector: [-3,0,2,4,7,10,12]
and this matrix:
[[ 7. 7. 4. ]
[12. 10. 10. ]
[-3. 7. 2. ]
[10. 8. 12. ]
[ nan 7. nan]
[ 7. 7. 10. ]
[ 4.5 nan 2. ]
[ 2. 12. 4. ]]
I have to show where in the matrix the the values are equal to in the vector. So it would show me which row and column 8 and 4.5 are e.g.
Any quick way to do this?
Answers:
You can use numpy broadcasting to compare each value with the whole 2d array, and then get indexes using numpy.argwhere:
import numpy as np
v = np.array([-3, 0, 2, 4, 7, 10, 12])
m = np.array([[ 7. , 7. , 4. ],
[12. , 10. , 10. ],
[-3. , 7. , 2. ],
[10. , 8. , 12. ],
[ nan, 7. , nan],
[ 7. , 7. , 10. ],
[ 4.5, nan, 2. ],
[ 2. , 12. , 4. ]]))
t = m[None,...] == v[:,None,None]
np.argwhere(t)
array([[0, 2, 0],
[2, 2, 2],
[2, 6, 2],
[2, 7, 0],
[3, 0, 2],
[3, 7, 2],
[4, 0, 0],
[4, 0, 1],
[4, 2, 1],
[4, 4, 1],
[4, 5, 0],
[4, 5, 1],
[5, 1, 1],
[5, 1, 2],
[5, 3, 0],
[5, 5, 2],
[6, 1, 0],
[6, 3, 2],
[6, 7, 1]], dtype=int64)
Let’s use the first row [0, 2, 0]
of the result to understand what represents:
- The first column is the index of the value in
v
that we are looking at, i.e. v[0] = -3
.
- The second and third column are the indexes in which the value in
m
is equal to such value in v
, i.e. m[2,0] = -3
.
If in the first column a index doesn’t appear it means that such value is not present in m
(e.g. 0 is not in m
) while if a index appears multiple times it means that such value is present more than once in m
(e.g. the value v[2] = 2
).
if it’s a small array like in your example, a simple iteration over the rows and columns would do… like so:
import numpy as np
v = np.array([-3, 0, 2, 4, 7, 10, 12])
m = np.array([[ 7. , 7. , 4. ],
[12. , 10. , 10. ],
[-3. , 7. , 2. ],
[10. , 8. , 12. ],
[ np.NaN, 7. , np.NaN],
[ 7. , 7. , 10. ],
[ 4.5, np.NaN, 2. ],
[ 2. , 12. , 4. ]])
#Iterations starts here
index_list = []
for i in range(m.shape[0]):
for j in range(m.shape[1]):
if m[i][j] not in v:
index_list.append((i,j))
The output of this code (if you print index_list
) is:
[(3, 1), (4, 0), (4, 2), (6, 0), (6, 1)]
I have this vector: [-3,0,2,4,7,10,12]
and this matrix:
[[ 7. 7. 4. ]
[12. 10. 10. ]
[-3. 7. 2. ]
[10. 8. 12. ]
[ nan 7. nan]
[ 7. 7. 10. ]
[ 4.5 nan 2. ]
[ 2. 12. 4. ]]
I have to show where in the matrix the the values are equal to in the vector. So it would show me which row and column 8 and 4.5 are e.g.
Any quick way to do this?
You can use numpy broadcasting to compare each value with the whole 2d array, and then get indexes using numpy.argwhere:
import numpy as np
v = np.array([-3, 0, 2, 4, 7, 10, 12])
m = np.array([[ 7. , 7. , 4. ],
[12. , 10. , 10. ],
[-3. , 7. , 2. ],
[10. , 8. , 12. ],
[ nan, 7. , nan],
[ 7. , 7. , 10. ],
[ 4.5, nan, 2. ],
[ 2. , 12. , 4. ]]))
t = m[None,...] == v[:,None,None]
np.argwhere(t)
array([[0, 2, 0],
[2, 2, 2],
[2, 6, 2],
[2, 7, 0],
[3, 0, 2],
[3, 7, 2],
[4, 0, 0],
[4, 0, 1],
[4, 2, 1],
[4, 4, 1],
[4, 5, 0],
[4, 5, 1],
[5, 1, 1],
[5, 1, 2],
[5, 3, 0],
[5, 5, 2],
[6, 1, 0],
[6, 3, 2],
[6, 7, 1]], dtype=int64)
Let’s use the first row [0, 2, 0]
of the result to understand what represents:
- The first column is the index of the value in
v
that we are looking at, i.e.v[0] = -3
. - The second and third column are the indexes in which the value in
m
is equal to such value inv
, i.e.m[2,0] = -3
.
If in the first column a index doesn’t appear it means that such value is not present in m
(e.g. 0 is not in m
) while if a index appears multiple times it means that such value is present more than once in m
(e.g. the value v[2] = 2
).
if it’s a small array like in your example, a simple iteration over the rows and columns would do… like so:
import numpy as np
v = np.array([-3, 0, 2, 4, 7, 10, 12])
m = np.array([[ 7. , 7. , 4. ],
[12. , 10. , 10. ],
[-3. , 7. , 2. ],
[10. , 8. , 12. ],
[ np.NaN, 7. , np.NaN],
[ 7. , 7. , 10. ],
[ 4.5, np.NaN, 2. ],
[ 2. , 12. , 4. ]])
#Iterations starts here
index_list = []
for i in range(m.shape[0]):
for j in range(m.shape[1]):
if m[i][j] not in v:
index_list.append((i,j))
The output of this code (if you print index_list
) is:
[(3, 1), (4, 0), (4, 2), (6, 0), (6, 1)]