How to convert all inf values into zero value

205 次查看(过去 30 天)
I sometimes generate values which are inf. And it appears all over the code I want to catch all that occurances of Inf and convert it to zero value whenever it appears. Can i do that?

回答(1 个)

Awais Saeed
Awais Saeed 2021-8-16
A = [3 1 24 inf; 65 21 56 12;inf inf 231 inf;0 12 inf 231];
A(isinf(A)) = 0 % find inf values and replace with 0
  3 个评论
Awais Saeed
Awais Saeed 2021-8-16
Sorry I do not know how to replace inf in equations
Walter Roberson
Walter Roberson 2021-8-16
Do you mean that you are using the Symbolic Toolbox and you are generating symbolic expressions that have symbolic inf ?
Or do you mean that you are dynamically overflowing numerically and you want to set inf instead?
In order to override inf dynamically, you would have to cheat on "dbstop if naninf" and it would be pretty tricky to do.
For example if you have
A = [realmax/10 realmax 2]
sum(A)
then adding realmax/10 + realmax would overflow to infinity, and you would want that overflow replaced with 0, and then you would want that 0 added to the 2, for a net sum() of 2. But MATLAB is permitted to sum() in any order (it breaks large sum() into pieces and does the pieces on multiple cores in parallel), so the realmax+2 part might be done first, which would give realmax as the result because 2 is far smaller than eps(realmx). Then the realmax/10 would be added to that realmax result, giving overflow that you would want to replace with 0, giving a net result for the sum() of 0... obviously not consistent with the 2 just talked about.
(inf+inf)*0
should give... (0+0)*0 = 0 ?? Rather than giving inf*0 = NaN ??

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

产品


版本

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by