필터 지우기
필터 지우기

Trying to remove Nans when plotting histogram, pdf and cdf

조회 수: 4 (최근 30일)
Nathaniel Porter
Nathaniel Porter 2022년 2월 25일
댓글: Nathaniel Porter 2022년 2월 26일
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에서 Histograms에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by