具有多个元素的数组的真值是不明确的.使用a.any()或a.all() [英] The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

查看:117
本文介绍了具有多个元素的数组的真值是不明确的.使用a.any()或a.all()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尽管我知道有各种各样的问题正在寻找解决此错误消息的方法,但我还没有找到一个可以帮助我解决代码以使比较正常工作的答案,但是我有代码

although I know there's various questions looking for solutions to this error message, I've yet to find an answer that helps me solve my code to get the comparison working, I have the code

def f(x,d,h,L):
    ans=0.
    if ((0.<=x) & (x<d)):
        ans=h*(x/d)
    elif ((d<=x) & (x<=L)):
        ans=((L-x)/(L-d))
    return ans

x=np.linspace(0,10,1000)
h=5*10**(-3)
d=16*10**(-2)
L=64.52*10**(-2)
func=f(x,d,h,L)

但是当我尝试运行它时,我得到一个错误,该错误指向标题中带有错误代码的if行,我已经尝试了类似问题中给出的建议解决方案,例如使用np.logical_andand &的值,但三个都产生相同的错误,请帮帮我

But when I try running it I get an error pointing to the if line with the error code in the title, I've tried the proposed solutions given in similar questions such as using np.logical_and or and instead of & but all three yield the same error, please help me out

推荐答案

使用numpy.where. (可选)对浮点数使用指数表示法.

Use numpy.where. Optionally, use exponential notation for floating point numbers.

import numpy as np

def f(x, d, h, L):
    return np.where(x < d, h*(x/d), (L - x)/(L - d))

x = np.linspace(0,10,1000)
h = 5e-3
d = 16e-2
L = 64.52e-2

func = f(x, d, h, L)

这篇关于具有多个元素的数组的真值是不明确的.使用a.any()或a.all()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆