MATLAB: How to exclude NaN, NaT and Inf from the total number of elements in histogram “normalization”

histograminfMATLABnannat;normalization

If I have a matrix A, and I want to plot its probability distribution in a histogram I use:
>> A = [NaN Inf 10 20]
>> histogram(A,'Normalization','probability')
However, what I get is a probability of 0.25 percent for "10" and of 0.25 percent for "20". This is because histogram counts NaN and Inf as elements so there are 4 total elements. But I want to ignore missing entries in the total number of elements.
Is there an option of the function "histogram" that allows me to exclude non-numerical values?

Best Answer

As of MATLAB R2019a, this option is not available. An enhancement request has been submitted. As a workaround you can use the following:
  • If "A" is numeric this can be done like this:
>> histogram(A (~( isnan(A)|isinf(A) ) ) ,'Normalization','probability')
  • If "A" is datetime this can be done like this:
>> histogram(A (~ isnat(A) ) ,'Normalization','probability')