To find the average when there is NaN
1 view (last 30 days)
I want to find the average of each pairs. ie. avg of 2 &4, avg of NaN and 5. Answer should be 3 and 5. For the second pair, code should eliminate NaN, so avg should be 5. That's where I have the problem. Following is the code I wrote. Thanks.
A=[2 4 NaN 5];
Chad Greene on 17 Aug 2017
Edited: Chad Greene on 17 Aug 2017
On newer versions of Matlab, use
If you have an older version of Matlab and you have the Statistics Toolbox, use
Alternatively, if neither of those options work for you,
tmp = A(C);
test(j)= = mean(tmp(isfinite(tmp)));
Image Analyst on 17 Aug 2017
Edited: Image Analyst on 17 Aug 2017
This will work for any length of A, even odd numbers:
A=[2 4 NaN 5 6 8 9]; % 7 elements.
kernel = [1,1];
sumA = conv(A, kernel, 'same')
nonNaNA = ~isnan(A)
denom = conv(nonNaNA, kernel, 'same')
output = sumA ./ denom
output(isnan(output)) =  % Remove nans.