Find values and replace them with NaN, add total number of NaN values.

131 views (last 30 days)
Hello! So within 3 different columns of data which I have defined as:
mintemp = b(:,4)
maxtemp = b(:,5)
avgtemp = b(:,6)
I want to take each individual row (1 column at a time) and find the -9999 values which are NaN values and replace them with 'NaN' so that when I calculate the average of one it doesn't skew the actual value, or find a way to calculate the average only using positive integers in Matlab if there is this function. Additionally, I would like to be able to keep a count of the total number of -9999 (NaN) values in that column so that I know how many missing values I have in the calculated average.
n=length(source_files);
for j= 1:n
mintemp = b(:,4)
NaN = -9999
ff= find(mintemp==NaN);
%This is about as far as I've got so far
f=ff+1
Any suggestions are appreciated! Thanks for your help everyone!

Accepted Answer

Dan Seal
Dan Seal on 17 Jul 2013
To replace all -9999 values with NaN, you can do:
mintemp(mintemp == -9999) = NaN;
If you have Statistics Toolbox, you can then use NANMEAN:
nanmean(mintemp)
If you don't have Statistics Toolbox, you can take the mean of the values that are not NaN:
mean(mintemp(~isnan(mintemp)))
If you don't want to replace things with NaN, and want to compute the average for only positive numbers, you can do this all in one command:
mean(mintemp(mintemp>0))
  4 Comments
Theodore
Theodore on 17 Jul 2013
Sorry had to refresh to see the comment on getting a count for my -9999/NaN values, which is the only other thing I was waiting on. Worked like a charm! Thanks a ton again!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!