How to calculate Average without receiving 'Inf' as a result.

9 views (last 30 days)
I am trying to find the average efficiency of this 1 column in MATLAB. I have tried N = mean(EstimatedEff,"omitnan") but get 'Inf' asa result. How can I change my code so I recieve a numerical figure for average efficiency? I have attached the Excel sheet below for reference and Using 2020b. Thankyou!

Accepted Answer

Sameer
Sameer on 9 Oct 2024
Edited: Sameer on 9 Oct 2024
Hi Mahnoor
It could be due to the presence of Inf values in your "EstimatedEff" array. The "omitnan" option only ignores NaN values, not Inf. To handle Inf values, you need to remove or replace them before calculating the mean.
You can modify the code as follows:
% Remove Inf values
EstimatedEff = EstimatedEff(~isinf(EstimatedEff));
% Calculate the mean, omitting NaN values
N = mean(EstimatedEff, 'omitnan');
Please refer to the below MathWorks documentation link:
Hope this helps!
  1 Comment
Steven Lord
Steven Lord on 9 Oct 2024
That would work if EstimatedEff is a vector or if you want the mean over the whole array. I'd probably use standardizeMissing to replace the Inf values with NaN and then use mean with 'omitnan' on that array (including specifying a dimension if necessary.)
EstimatedEff = randi(10, 5, 5);
nonfinitelocations = randperm(25, 10);
EstimatedEff(nonfinitelocations(1:4)) = NaN;
EstimatedEff(nonfinitelocations(5:7)) = Inf;
EstimatedEff(nonfinitelocations(8:10)) = -Inf
EstimatedEff = 5×5
6 9 7 10 9 6 5 NaN 10 1 NaN 5 -Inf Inf 8 Inf NaN 1 -Inf NaN 10 -Inf 1 4 Inf
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Replace all the Inf and -Inf values with NaN
E = standardizeMissing(EstimatedEff, [Inf, -Inf])
E = 5×5
6 9 7 10 9 6 5 NaN 10 1 NaN 5 NaN NaN 8 NaN NaN 1 NaN NaN 10 NaN 1 4 NaN
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
mean(E, "omitnan")
ans = 1×5
7.3333 6.3333 3.0000 8.0000 6.0000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
mean(E, 2, "omitnan")
ans = 5×1
8.2000 5.5000 6.5000 1.0000 5.0000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
mean(E, "all", "omitnan")
ans = 6.1333

Sign in to comment.

More Answers (2)

Star Strider
Star Strider on 9 Oct 2024
Uz = unzip('Matlab Copy.zip')
Uz = 1x1 cell array
{'Matlab Copy.csv'}
T1 = readtable(Uz{1}, 'VariableNamingRule','preserve')
T1 = 674835x1 table
Estimated Eff. ______________ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
NonZeros = nnz(T1{:,1})
NonZeros = 580446
NrInf = nnz(isinf(T1{:,1}))
NrInf = 29
NrNaN = nnz(isnan(T1{:,1}))
NrNaN = 0
NrFinite = nnz(isfinite(T1{:,1}))
NrFinite = 674806
mean_of_finite_values = mean(T1{isfinite(T1{:,1}),:})
mean_of_finite_values = 0.8364
locs = isfinite(T1{:,1}); % Alternative Approach
NrNonfinite = nnz(~locs)
NrNonfinite = 29
mean_of_finite_values = mean(T1{locs,:})
mean_of_finite_values = 0.8364
Of the 674835 values in the file, there are 0 NaN values, 29 are Inf and 674506 are finite.
.
  1 Comment
Star Strider
Star Strider on 9 Oct 2024
Using the 'omitnan' flag is absolutely pointless in this instance!
The reason is that there are no NaN values at all, only Inf and finite values!

Sign in to comment.


Aquatris
Aquatris on 9 Oct 2024
Here is one way:
% create dummy matrix
A = rand(100,1);
A([5 10 24 55 23 60]) = inf;
A([12 56 29 35]) = nan;
mean(A) % simple mean
ans = NaN
mean(A,'omitnan') % omitting nan s
ans = Inf
mean(A(~isinf(A)),'omitnan') % omitting nan and inf -inf
ans = 0.4871

Community Treasure Hunt

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

Start Hunting!