I need to work with multiple condition using numpy.
I'm trying this code that seem to work.
My question is: There is another alternative that can do the same job?
Mur=np.array([200,246,372])*pq.kN*pq.m
Mumax=np.array([1400,600,700])*pq.kN*pq.m
Mu=np.array([100,500,2000])*pq.kN*pq.m
Acreq=np.where(Mu<Mur,0,"zero")
Acreq=np.where(((Mur<Mu)&(Mu<Mumax)),45,Acreq)
Acreq=np.where(Mu>Mumax,60,Acreq)
Print(Acreq)
['0' '45' '60']
Starting with this:
Mur = np.array([200,246,372])*3*5
Mumax = np.array([1400,600,700])*3*5
Mu = np.array([100,500,2000])*3*5
Acreq = np.where(Mu<Mur,0,"zero")
Acreq = np.where((Mur<Mu)&(Mu<Mumax),45,Acreq)
Acreq = np.where(Mu>Mumax,60,Acreq)
print(Acreq)
['0' '45' '60']
Try this:
conditions = [Mu<Mur, (Mur<Mu)&(Mu<Mumax), Mu>Mumax ]
choices = [ 0, 45, 60 ]
Acreq = np.select(conditions, choices, default='zero')
print(Acreq)
['0' '45' '60']
This also works:
np.where((Mur<Mu)&(Mu<Mumax),45,np.where(Mu>Mumax,60,np.where(Mu<Mur,0,"zero")))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With