Trying to remove Nans when plotting histogram, pdf and cdf

9 次查看(过去 30 天)
clear;
load InsulinReadings.mat
xX2 = InsulinReadings;
xX2(xX2==0)=missing;
A2 = mean(xX2,'all',"omitnan")
B2 = median(xX2,'all',"omitnan")
C2 = max(xX2,[],'all',"omitnan")
D2 = min(xX2,[],'all', "omitnan")
figure
histogram(InsulinReadings(~isnan(InsulinReadings),128,'Normalization')
Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters.
xlabel('Insulin ng/dL')
%Now get pdf
[D PD] = allfitdist(xX2,'PDF');
xlabel('Insulin ng/dL');
%Now get the CDF
[D PD] = allfitdist(xGlucoseReadings,'CDF');
xlabel('Insulin ng/dL')

采纳的回答

Voss
Voss 2022-2-25
I think you basically have it right. I just "fixed" a syntax error on the line where you call histogram(). ("fixed" is in quotes because I can't be sure what you're going for there.)
(Also, looks like allfitdist.m has been removed from the File Exchange, so I can't run it, but maybe your copy does the right thing here - I don't know.)
clear;
load InsulinReadings.mat
xX2 = InsulinReadings;
xX2(xX2==0)=missing;
A2 = mean(xX2,'all',"omitnan")
A2 = 296.9102
B2 = median(xX2,'all',"omitnan")
B2 = 86.4899
C2 = max(xX2,[],'all',"omitnan")
C2 = 1.0226e+04
D2 = min(xX2,[],'all', "omitnan")
D2 = 3.6900e-07
figure
histogram(InsulinReadings(~isnan(InsulinReadings)),128)%,'Normalization')
xlabel('Insulin ng/dL')
%Now get pdf
[D PD] = allfitdist(xX2,'PDF');
Unrecognized function or variable 'allfitdist'.
xlabel('Insulin ng/dL');
%Now get the CDF
[D PD] = allfitdist(xGlucoseReadings,'CDF');
xlabel('Insulin ng/dL')
  6 个评论
Voss
Voss 2022-2-25
There probably are built-in functions, but I don't know what they are off the top of my head (maybe search the documentation).
It's relatively straighforward to calculate a PDF and CDF from the properties of the histogram:
clear;
load InsulinReadings.mat
xX2 = InsulinReadings;
xX2(xX2==0)=missing;
figure();
h = histogram(xX2(~isnan(xX2)),128);%,'Normalization')
% now make a new histogram with values in the first bin replaced with NaNs
edges = get(h,'BinEdges');
xX2(xX2 < edges(2)) = NaN;
figure();
h = histogram(xX2(~isnan(xX2)),128);%,'Normalization')
xlabel('Insulin ng/dL')
% pdf and cdf
figure();
edges = get(h,'BinEdges');
counts = get(h,'BinCounts');
bin_centers = (edges(1:end-1)+edges(2:end))/2;
total_counts = sum(counts);
pdf = counts/total_counts;
cdf = cumsum(counts)/total_counts;
plot(bin_centers,pdf,'LineWidth',2);
hold on
plot(bin_centers,cdf,'LineWidth',2);
xlim(bin_centers([1 end]));
legend('PDF','CDF');

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Distribution Plots 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by