Normalise a Histogram excluding NaN?
19 次查看(过去 30 天)
显示 更早的评论
Shown under M1, i am attempting to plot a histogram and it's Normal Distribution, however the data has a large quantity of NaN. I was able to omit them in the plotting for the line but not the histogram bars themselves.
Does anyone know how to fix this?

0 个评论
采纳的回答
Star Strider
2018-1-6
One option is:
histogram(m(~isnan(m)), 50, 'Normalization','PDF')
This eliminates the NaN values from your data before doing the analysis.
Experiment to get the result you want.
0 个评论
更多回答(1 个)
Erik Giesen Loo
2018-9-19
Using a deprecated method, but may be useful to compare the validity of MATLAB's histogram. I start by defining a function that finds the length of a vector ignoring all nan's (similar to how nanmean and nanstd work).
function nx = nanlength(x)
% nanlength(x) returns the number of elements of vector x that are not nan.
dim = find(size(x) ~= 1, 1);
if dim == 2
x = x(:)';
else % dim == 1
x = x(:);
end
xnans = isnan(x);
if any(xnans(:))
nx = sum(~xnans,dim);
else
nx = size(x,dim); % a scalar, => a scalar call to tinv
end
Now we proceed to implement the pdf normalization ourselves:
[y,x] = hist(data,m); % m = number of bins using any desired algorithm
width = x(2) - x(1);
y_bar = y/(nanlength(data)*width);
bar(x,y_bar,'hist')
It might be nice if MATLAB implemented an option to ignore NAN's like in many other functions.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Histograms 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!