How to convert all inf values into zero value

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?

Respuestas (1)

Awais Saeed
Awais Saeed el 16 de Ag. de 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

3 comentarios

Ghaith Mustafa
Ghaith Mustafa el 16 de Ag. de 2021
The inf values in my code appears in equations not in vector What i want is to set all inf appears in each equation into zero
Awais Saeed
Awais Saeed el 16 de Ag. de 2021
Sorry I do not know how to replace inf in equations
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 ??

Iniciar sesión para comentar.

Categorías

Productos

Versión

R2020a

Etiquetas

Preguntada:

el 16 de Ag. de 2021

Comentada:

el 16 de Ag. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by