MATLAB: Calculating PDF from data set

normal distributionpdfplotting

I have a random variable k whose size is (1000*1), I want to draw a PDF curve for its values over a range x.
I tried this line:
pdf_normal = pdf('Normal',k,x);
But it return an array all of its values are NaN.
How can I fix that? or is there another way to get what I want?
Note:
k and x have the same size.

Best Answer

That is likely not the appropriate initial function. The fitdist function would likely do what you want. Then, use the pdf function on the output of fitdist.
Example —
k = randn(1000,1); % Create ‘k’
x = linspace(-5,5); % Create ‘x’
pd = fitdist(k, 'Normal');
y = pdf(pd,x);
figure
plot(x, y)
grid
xlabel('x')
ylabel('PDF')
.