how can I change the code to have only unique CDF and PDF plot?not for all samples

1 view (last 30 days)
mu = 1;
sigma = 5; % make the distribution as wide as we want.
N = 100;
randgeneration = randn(N,1)*sigma + mu;
pdfNormal = normpdf(randgeneration, mu, sigma);
figure;
subplot(2,2,1)
plot(randgeneration, pdfNormal);
xlabel('randomgeneration');
ylabel('pdfNormal');
subplot(2,2,2)
histogram(randgeneration);
subplot(2,2,3)
histfit(randgeneration);
subplot(2,2,4);
pd=makedist('Normal'); %create probability distribution object
cumulativedis=cdf(pd,randgeneration);
plot(randgeneration,cumulativedis,'r-.');
xlabel('randgeneration');
ylabel('CDF');
disp(mean(randgeneration));
disp(std(randgeneration))

Accepted Answer

Star Strider
Star Strider on 16 May 2022
I am not absolutely certain what you want.
If you want one plot for the first and last subplots, rather than multiple lines in each one, sort them by first sorting ‘randgeneration’ and using that index for it and the others (the second and third subplots are histograms, so the order is irrelevant for them) —
mu = 1;
sigma = 5; % make the distribution as wide as we want.
N = 100;
randgeneration = randn(N,1)*sigma + mu;
[~,ix] = sort(randgeneration); % Create Sorting Index
pdfNormal = normpdf(randgeneration, mu, sigma);
figure;
subplot(2,2,1)
plot(randgeneration(ix), pdfNormal(ix));
xlabel('randomgeneration');
ylabel('pdfNormal');
subplot(2,2,2)
histogram(randgeneration);
subplot(2,2,3)
histfit(randgeneration);
subplot(2,2,4);
pd=makedist('Normal'); %create probability distribution object
cumulativedis=cdf(pd,randgeneration);
plot(randgeneration(ix),cumulativedis(ix),'r-.');
xlabel('randgeneration');
ylabel('CDF');
disp(mean(randgeneration));
1.2960
disp(std(randgeneration))
4.6708
.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!