【发布时间】:2013-11-06 06:44:58
【问题描述】:
我对编程很陌生。我在 matlab 中有代码:
x2(x2>=0)=1;
x2(x2<0)=-1;
%Find values in x2 which are less than 0 and replace them with -1,
%where x2 is an array like
0,000266987932788242
0,000106735120804439
-0,000133516844874253
-0,000534018243439120
我尝试在 Python 中使用代码来做到这一点
if x2>=0:
x2=1
if x2<0:
x2=-1
这会返回ValueError:具有多个元素的数组的真值是不明确的。使用 a.any() 或 a.all()
我应该怎么做才能让所有的正面都被 1 取代,而负面则被 -1 取代> 和 STORE 所有这些都在 x2 中,例如,不只是打印,以便我可以使用它稍后再做一些其他的事情。
【问题讨论】:
-
现在更新了我的答案。请检查。
标签: python arrays matlab for-loop numpy