在python中我有一个矩阵,我需要将相同的矩阵返回给我,除了我有一个规则,如果该矩阵中的元素是< 0我将它们各自的值乘以常量。我不知道如何去做这件事。
示例:a = [[0,2,1,4],[ - 2,3,5,2]]并且假设我的常量是-0.1,那么我将返回a = [[0,2] ,1,4],[。2,3,5,2]
答案 0 :(得分:1)
演示:
In [55]: a = np.random.randint(-10, 10, size=(10,10))
In [56]: a
Out[56]:
array([[ 7, 6, 0, 2, 3, -9, 2, -2, 9, -10],
[ 8, 4, -10, 5, 7, 6, 7, -3, 1, -3],
[ 5, -10, -8, 4, -2, -9, 0, 8, -1, 7],
[ 6, 7, 6, 2, -3, 3, 0, -7, -6, -4],
[ 8, 0, -7, 7, 9, -4, -5, 7, -5, -9],
[-10, -9, -6, -9, -1, 2, -6, -9, 8, -3],
[ 5, -3, -6, -5, 6, -8, -10, 7, 3, -5],
[ 9, 4, 5, 9, 2, -5, -8, 5, -1, -7],
[ -9, -7, -7, -3, -10, -7, 3, -1, 5, 3],
[ 0, -4, 9, -9, -5, -1, -8, 9, -4, -5]])
In [57]: a[a<0] *= 10
In [58]: a
Out[58]:
array([[ 7, 6, 0, 2, 3, -90, 2, -20, 9, -100],
[ 8, 4, -100, 5, 7, 6, 7, -30, 1, -30],
[ 5, -100, -80, 4, -20, -90, 0, 8, -10, 7],
[ 6, 7, 6, 2, -30, 3, 0, -70, -60, -40],
[ 8, 0, -70, 7, 9, -40, -50, 7, -50, -90],
[-100, -90, -60, -90, -10, 2, -60, -90, 8, -30],
[ 5, -30, -60, -50, 6, -80, -100, 7, 3, -50],
[ 9, 4, 5, 9, 2, -50, -80, 5, -10, -70],
[ -90, -70, -70, -30, -100, -70, 3, -10, 5, 3],
[ 0, -40, 9, -90, -50, -10, -80, 9, -40, -50]])