# How to convert all inf values into zero value

135 views (last 30 days)
Ghaith Mustafa on 16 Aug 2021
Commented: Walter Roberson on 16 Aug 2021
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?

Awais Saeed on 16 Aug 2021
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
Walter Roberson on 16 Aug 2021
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 ??

R2020a

### Community Treasure Hunt

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

Start Hunting!