How to replace the array elements with "Nan" or "Inf" to either zero or some other number?
284 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
raghavendra kandukuri
el 25 de Oct. de 2018
Comentada: Steven Lord
el 17 de Jun. de 2021
AdjSpeed, THR, Thrtbl, AdjIndTorque are the different fields of data going in to the calculation, all the fields return the values except the two values in AdjIndTorque which are returning "Nan" (PFA Excel, line 2456 and 2457). [e,f] = size(AdjSpeed) i=1; while i<(e+1) k=interp2(IT(1,:),THR,ThrTbl,AdjSpeed(i,1),THR); if (isnan(AdjIndTorque(i,1))) AdjIndTorque(i,1) = 0; end yout2(i,1)=interp1(k,THR,AdjIndTorque(i,1)); i=i+1; end
Console O/P:
Error using griddedInterpolant, The coordinates of the input points must be finite values; Inf and NaN are not permitted.
Error in FTCC>pushbutton3_Callback (line 439) yout2(i,1)=interp1(k,THR,AdjIndTorque(i,1));
I tries some thing with 'isnan' but its not doing anything
0 comentarios
Respuesta aceptada
the cyclist
el 25 de Oct. de 2018
I don't fully understand everything you wrote, but here is how to do the thing you asked in the title:
x = [-Inf -3 0 3 Inf NaN]; % Some input data
x(isinf(x)|isnan(x)) = 0; % Replace NaNs and infinite values with zeros
x =
0 -3 0 3 0 0
5 comentarios
the cyclist
el 17 de Jun. de 2021
% Create two identical input arrays
x1 = [Inf NaN 2 3 4 5];
x2 = x1;
% Use this syntax to replace Inf and NaN with zero:
x1(isinf(x1)|isnan(x1)) = 0
% Use this syntax to *remove* Inf and NaN values, resulting in a shorter array:
x2(isinf(x2)|isnan(x2)) = []
Steven Lord
el 17 de Jun. de 2021
The former replaces non-finite values in x with 0. After that command runs, x is the same size and shape as it was previously.
The latter removes non-finite values in x. After that command runs, x is likely to be a different size and shape.
I'm going to create an x vector and then manipulate it in three ways: the two about which you asked and one using a slightly different condition to create the logical indices.
x = [1:5 Inf 7 NaN 9:10]
x1 = x;
x1(isinf(x1) | isnan(x1)) = 0
x2 = x;
x2(isinf(x2) | isnan(x2)) = []
x3 = x;
x3(~isfinite(x3)) = 0
x, x1, and x3 are the same length. x2 is shorter.
Más respuestas (0)
Ver también
Categorías
Más información sobre Multidimensional Arrays en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!