我正在使用Python。我有一个数组(如下)
overflow-y: scroll;
height: 13em; // Initial height.
resize: vertical; // Allow user to change the vertical size.
max-height: 31em; // If you want to constrain the max size.
我正在寻找编写代码,该代码将采用每行的第一个元素,并从中减去该行中的每个值。
例如,第1行:20.28466797-20.28466797、19.24307251- 20.28466797、20.87997437-20.28466797等。 第2行:4.21954346 -4.21954346、17.05456543-4.21954346等
答案 0 :(得分:3)
您可以使用numpy.tile重复每一行的第一个元素以创建矩阵并将其从原始矩阵中减去。
your_matrix - np.tile(your_matrix[:,:1], your_matrix.shape[0])
答案 1 :(得分:1)
以下将为您完成这项工作:
import numpy as np
def array_fun(arr):
# compute the length of the given array
n = len(arr)
m = len(arr[0])
# create an empty list
aList = []
# append by substracting the first element
[aList.append(arr[i][j]-arr[i][0]) for i in range(n) for j in range(m)]
# return modified array
return np.array(aList).reshape(n,m)
if __name__ == "__main__":
# define your array
arr = [[1, 3, 7], [1, 1, 2], [5, 2, 2]]
# print initial array
print(np.array(arr))
# print modified array
print(np.array(array_fun(arr)))
初始数组:
[[1 3 7]
[1 1 2]
[5 2 2]]
最终数组:
[[ 0 2 6]
[ 0 0 1]
[ 0 -3 -3]]