从两个矩阵的比较中创建新矩阵
Create new matrices from the comparison of two matrices
我有两个矩阵 X1
和 X2
表示多项式的复解:
X1=(N.t[:,1,1]-N.t[:,0,0]-np.sqrt(delta))/2*(N.t[:,1,0])
X2=(N.t[:,1,1]-N.t[:,0,0]+np.sqrt(delta))/2*(N.t[:,1,0])
我正在尝试创建两个新矩阵 C1
和 C2
:C1
必须包含最大值:max(X1, X2)
和 C2
必须包含最小值:min(X1, X2)
。我使用 np.abs
来比较值,因为它们很复杂,但我不知道如何进行索引。
有人可以帮助我吗?
要比较复数,您首先需要使用它们的绝对值将它们“转换”为实数:
X1_real, X2_real = np.abs(X1), np.abs(X2)
然后,你可以简单地使用numpy的where
函数,如下:
boolean_array = (X1_real >= X2_real) # Or >, as you want.
C1 = np.where(boolean_array, X1, X2) # If X1_real[i] is >= X2_real[i], then C1[i] will be equal to X1[i], otherwise to X2[i].
C2 = np.where(boolean_array, X2, X1) # The opposite here.
这与您寻找的最大和最小数组相匹配!
我有两个矩阵 X1
和 X2
表示多项式的复解:
X1=(N.t[:,1,1]-N.t[:,0,0]-np.sqrt(delta))/2*(N.t[:,1,0])
X2=(N.t[:,1,1]-N.t[:,0,0]+np.sqrt(delta))/2*(N.t[:,1,0])
我正在尝试创建两个新矩阵 C1
和 C2
:C1
必须包含最大值:max(X1, X2)
和 C2
必须包含最小值:min(X1, X2)
。我使用 np.abs
来比较值,因为它们很复杂,但我不知道如何进行索引。
有人可以帮助我吗?
要比较复数,您首先需要使用它们的绝对值将它们“转换”为实数:
X1_real, X2_real = np.abs(X1), np.abs(X2)
然后,你可以简单地使用numpy的where
函数,如下:
boolean_array = (X1_real >= X2_real) # Or >, as you want.
C1 = np.where(boolean_array, X1, X2) # If X1_real[i] is >= X2_real[i], then C1[i] will be equal to X1[i], otherwise to X2[i].
C2 = np.where(boolean_array, X2, X1) # The opposite here.
这与您寻找的最大和最小数组相匹配!