Duck
Duck

Reputation: 53

Plot a Cumulative Distribution Function in MATLAB

I'm getting a strange looking graph from my cdf function. If I use ecdf, I get the graph I expect. But I get a tangled mess that looks like it contains the right data, but in some wrong order.

SNR = exprnd(1,1000,1); 
Cap = 1*log2(1+SNR); % unit bandwidth

[f,x] = ecdf(Cap);
figure(2);
plot( x,f);

cdf_Cap = cdf('Exponential', Cap, 1);
figure(3);
plot( Cap, cdf_Cap);

figure(4);
cdfplot(Cap);

Figure 2 shows the expected result: enter image description here

and Figure 3 shows: enter image description here

I'm sure its the right data, and just requires some kind of absolute function, or sorting function. I just have no idea what that would be. Any help would be much appreciated.

Upvotes: 2

Views: 1622

Answers (1)

Rafael Monteiro
Rafael Monteiro

Reputation: 4549

Looks like Cap is not monotonically increasing. I think you might sort it before plotting.

On figure(3), replace this:

plot( Cap, cdf_Cap);

With this:

[~, idx] = sort(Cap);
plot( Cap(idx), cdf_Cap(idx));

Now the data will be plotted in the correct order.

Upvotes: 4

Related Questions