Reputation: 135
I want to check if all the rows of the matrix contain at least one negative element. If so, change the signs of all the elements of the matrix.
Here is my attempt, but this way just changes the sign of the negative elements:
matrix = [[5, -6, 2], [7, -2, 3], [8, 4, -9]]
for i in range(len(matrix)):
for j in range(len(matrix[i])):
if matrix[i][j] < 0:
print(matrix[i][j]*(-1))
Upvotes: 1
Views: 1163
Reputation: 28606
A nice way to check whether all rows contain a negative number (assuming your matrix is at least 1x1, not empty):
max(map(min, matrix)) < 0
So overall:
if max(map(min, matrix)) < 0:
matrix[:] = [[-x for x in row] for row in matrix]
Upvotes: 0
Reputation: 4528
The easiest way that comes to my mind :
m = [[5,-6, 2], [7, -2, 3], [8, 4, -9], [2 , 4 ,6]]
for i in range(len(m)):
flag = False
for y in m[i]:
if(y<0):
flag = True
break
if (flag == True):
for j in range(len(m[i])):
m[i][j] = -1 * m[i][j]
print (m) # [[-5, 6, -2], [-7, 2, -3], [-8, -4, 9], [2, 4, 6]]
Upvotes: 0
Reputation: 916
EDIT:
So first you can check if matrix contains at least one negative element, then change sign of all elements.
matrix = [[5, -6, 2], [7, 2, 3], [8, 4, -9]]
hasNegativeElement = False
for i in range(len(matrix)):
for j in range(len(matrix[i])):
if matrix[i][j] < 0:
hasNegativeElement = True
break
if hasNegativeElement:
for i in range(len(matrix)):
for j in range(len(matrix[i])):
matrix[i][j] *= (-1)
Upvotes: -1
Reputation: 1396
Try this:
allRowsNegative = True;
for i in range(len(matrix)):
foundNegative = False;
for j in range(len(matrix[i])):
foundNegative = (foundNegative or matrix[i][j] < 0);
allRowsNegative = (allRowsNegative and foundNegative);
if allRowsNegative:
for i in range(len(matrix)):
for j in range(len(matrix[i])):
print(matrix[i][j]*(-1));
else:
for i in range(len(matrix)):
for j in range(len(matrix[i])):
print(matrix[i][j]);
Upvotes: 0
Reputation: 78740
non numpy solution:
>>> matrix = [[5, -6, 2], [7, -2, 3], [8, 4, -9]]
>>> if all(any(x < 0 for x in sub) for sub in matrix):
... result = [[-x for x in sub] for sub in matrix]
...
>>> result
[[-5, 6, -2], [-7, 2, -3], [-8, -4, 9]]
numpy solution
>>> import numpy as np
>>> matrix = [[5, -6, 2], [7, -2, 3], [8, 4, -9]]
>>> m_arr = np.array(matrix)
>>> if np.all(np.any(m_arr < 0, axis=1)):
... result = (m_arr*(-1)).tolist()
...
>>> result
[[-5, 6, -2], [-7, 2, -3], [-8, -4, 9]]
Upvotes: 0
Reputation: 3106
You can check every row with np.any
for a negative element which would give an array and you can then check if all of them turned out to have a negative element via np.all
. If so reverse the sign of the matrix.
import numpy as np
A = np.array([[5, -6, 2], [7, 2, 3], [8, 4, -9]])
if np.all(np.any(A<0,axis=1)):
A *= -1
Upvotes: 0
Reputation: 31682
You could do it very easy with numpy
:
import numpy as np
In [108]: np.array(matrix) * -1
Out[108]:
array([[-5, 6, -2],
[-7, -2, -3],
[-8, -4, 9]])
Or if you want a list at the end you could use tolist
method:
In [124]: (np.array(matrix) * -1).tolist()
Out[124]: [[-5, 6, -2], [-7, -2, -3], [-8, -4, 9]]
Upvotes: 2