在列表中更改'0'以避免被0错误分隔

假设我有一个嵌套列表: {{2,0,3},{4,0,4},{0,0,9}} 。 我想将所有0更改为0.00001或其他一些小值,以便最终结果为{{2,0.00001,3},{4,0.00001,4},{0.00001,0.00001,9}}

If不使用ForIf函数,我会如何去做?

提前致谢。


也许更好的解决办法是允许零除,但抑制警告:

In[1]:= Quiet[1/{{2, 0, 3}, {4, 0, 4}, {0, 0, 9}}, Power::infy]
Out[1]= {{1/2,ComplexInfinity,1/3}, {1/4,ComplexInfinity,1/4}, {ComplexInfinity,ComplexInfinity,1/9}}

但我猜如果你想用0.00001替换Integer 0,那么你可以试试

In[2]:= {{2, 0, 3}, {4, 0, 4}, {0, 0, 9}} /. 0 -> 0.00001
       1/%
Out[2]= {{2,0.00001,3},{4,0.00001,4},{0.00001,0.00001,9}}
Out[3]= {{1/2,100000.,1/3},{1/4,100000.,1/4},{100000.,100000.,1/9}}

{{2, 0, 3}, {4, 0, 4}, {0, 0, 9}} /. {0 -> 0.0001}
(*
-> {{2,0.0001,3},{4,0.0001,4},{0.0001,0.0001,9}}
*)

然而,正如@nomulous所说,这不太可能是实现你所追求的最好方式。 那么,你想要做什么? 你试图解决的实际问题是什么?

编辑:在你的评论中,你提到你试图分割图像。 没有看到更多的代码,我会建议这样的事情:

i1 = Image[ {{0, 1, 0}, {1, 0, 1}, {0, 1, 0}}]
i2 = Image[ RandomReal[1, {3, 3}]]
Image[Quiet[i2[[1]]/i1[[1]]] /. ComplexInfinity -> 1]

它提取数据列表,进行分割,允许零除以产生ComplexInfinity ,将ComplexInfinity替换为1(允许的最大值),然后将Image包装在结果列表中。

也许如果你显示更多的代码,建议更好的东西会更容易。

链接地址: http://www.djcxy.com/p/20249.html

上一篇: Changing '0's in a list to avoid divide by 0 errors

下一篇: Plotting arrows at the edges of a curve