How to replace the array elements with "Nan" or "Inf" to either zero or some other number?

284 Ansichten (letzte 30 Tage)
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

Akzeptierte Antwort

the cyclist
the cyclist am 25 Okt. 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 Kommentare
the cyclist
the cyclist am 17 Jun. 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
x1 = 1×6
0 0 2 3 4 5
% Use this syntax to *remove* Inf and NaN values, resulting in a shorter array:
x2(isinf(x2)|isnan(x2)) = []
x2 = 1×4
2 3 4 5
Steven Lord
Steven Lord am 17 Jun. 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]
x = 1×10
1 2 3 4 5 Inf 7 NaN 9 10
x1 = x;
x1(isinf(x1) | isnan(x1)) = 0
x1 = 1×10
1 2 3 4 5 0 7 0 9 10
x2 = x;
x2(isinf(x2) | isnan(x2)) = []
x2 = 1×8
1 2 3 4 5 7 9 10
x3 = x;
x3(~isfinite(x3)) = 0
x3 = 1×10
1 2 3 4 5 0 7 0 9 10
x, x1, and x3 are the same length. x2 is shorter.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Multidimensional Arrays finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by