陷入阵列并根据元素单独应用公式的功能,该函数基于“ if”陈述
我有一系列的浮子,可以说“ array =([2.0、7.0、8.0、1.0])”,我需要创建一个第二个数组,该数组适用于第一个元素的公式(超出两个),一个 “如果”语句应用于第三个数组上的相应元素(可以说,array3 =([1,4,5,1]))。类似于“如果array3_element为< 3,而不是array2_element = array_element * 2。如果array3_element> 3,而不是array2_element = array_element * array_element * 3“。对于此示例,“数组”的结果将为“ array2 =([4.0,21.0,24.0,2.0])”。
下面的代码是我的最佳尝试,但它无法正常工作。它仅测试具有“ if”语句的第一个元素,并将相同选择的公式应用于角色数组。
As = np.array ([[20], [20]])
def Rsi (x, curv_adi):
for i in def_aco(x,curv_adi):
if abs(i) > epsilon_aco:
return (As/10000)*(Fsd*1000)
else:
return (As/10000)*(Fsd*1000)*((def_aco(x,curv_adi))/(epsilon_aco))
*def_aco(x,curv_adi)是代码中的一个函数,该函数在“ def_aco =([2.3275,0.1275])中导致,并表示上面示例上的“ array3”。就像“数组”和“ array2”将是函数结果。
I have an array of floats, lets say "array=([2.0, 7.0, 8.0, 1.0])", and I need to create a second array that applies a formula (out of two) to the elements of the first based on a
"if" statement applied to the respective element on a third array (let`s say, array3 = ([1, 4, 5, 1]) ). Something like "If array3_element is < 3, than array2_element = array_element * 2. If array3_element > 3, than array2_element = array_element * 3". For this example,the result for "array" would be "array2=([4.0, 21.0, 24.0, 2.0])".
The code below is my best attempt, but it doesn`t work as expected. It tests only the first element with the "if" statement and applies the same chosen formula to the role array.
As = np.array ([[20], [20]])
def Rsi (x, curv_adi):
for i in def_aco(x,curv_adi):
if abs(i) > epsilon_aco:
return (As/10000)*(Fsd*1000)
else:
return (As/10000)*(Fsd*1000)*((def_aco(x,curv_adi))/(epsilon_aco))
*def_aco (x, curv_adi) being a function up in the code that results in "def_aco = ([2.3275, 0.1275])" and represents the "array3" on the example above. As is "array" and "array2 would be the function result.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
让我们将代码转换为一般术语:
代码的问题是,您只会获得第一个元素,因为您从函数中返回而不是在所有数组上迭代。
您可以通过:
如果需要,可以使用列表理解:
因此,在您的情况下,您可以通过:
``````
let's translate your code to general terms:
The problem with the code is, that you will get only the first element, because you return from the function instead of iterate over all the array.
You can fix it by:
If you want, you can use list comprehension instead:
So, in your case you can solve it by:
`