Home > Software design >  Comparing array elements using mathematical symbols in Python
Comparing array elements using mathematical symbols in Python

Time:06-30

I have an array A. I am comparing A[i 1]=A[i] but the current output is in terms of 0's and 1's. Instead I want it to give in terms of < or = or >. I present the expected output.

import numpy as np

A=np.array([np.array([[0.01609   , 0.01728839, 0.01635707],
              [0.01696908, 0.01661592, 0.0163007 ],
              [0.01609   , 0.01642818, 0.01950431]]),
       np.array([[[0.01609   , 0.01728839, 0.01635707],
               [0.01696908, 0.01661592, 0.0163007 ],
               [0.01609   , 0.01642818, 0.01950431]]]),
       np.array([[[0.01609   , 0.017286  , 0.01635698],
               [0.01698125, 0.01659415, 0.01630078],
               [0.01627952, 0.01639007, 0.01951677]]])], dtype=object)


for i in range(0,len(A)-1):
    B=(A[i 1]==A[i]).astype(int)
    print([B])

The current output is

[array([[[1, 1, 1],
        [1, 1, 1],
        [1, 1, 1]]])]
[array([[[1, 0, 0],
        [0, 0, 0],
        [0, 0, 0]]])]

The expected output is

[array([[=, =, =],
        [=, =, =],
        [=, =, =]]])]
[array([[[=, <, <],
        [>, <, >],
        [>, <, >]]])]

CodePudding user response:

You can do it using np.select

for i in range(len(A)-1):
    B = np.select([A[i 1]==A[i],A[i 1]>A[i],A[i 1]<A[i]], ["=",">","<"])
    print([B])
  • Related