MATLAB: How to replace the array elements with “Nan” or “Inf” to either zero or some other number

forifinterpolationisnanwhile

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

Best Answer

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