how do I create a function
이전 댓글 표시
I have the code below to plot the kernel distribution of the data x1, x2,x3 where those x-s are vectors. But instead of changing the code
each time I have new data I want to create a function that runs each time I call it on the new data.
Also if you could help me to use as x-axis the values of +/-std of each data set instead of inisiating with s=(-3:0.01:3);
x1=(x1-mean(x1))/std(x1);
x2=(x2-mean(x2))/std(x2);
x3=(x3-mean(x3))/mean(x3);
% Creating empirical distribution and plot the results together with normal
% distribution N(0,1)
pd1=fitdist(x1,'Kernel');
pd2=fitdist(x2,'Kernel');
pd3=fitdist(x3,'Kernel');
pd4=makedist('Normal');
s=(-3:0.01:3);
y1=(pdf(pd1,s))';
y2=(pdf(pd2,s))';
y3=(pdf(pd3,s))';
y4=(pdf(pd4,s);
figure
figure,plot(s,y1,'-b',s,y2,'k',s,y3,'-g',s,y4,'-r')
댓글 수: 4
Rik
2020년 6월 10일
So you want to have code that allows you to input x4, x5, etc without having to edit the code?
gjashta
2020년 6월 10일
Walter Roberson
2020년 6월 10일
function gjastas_function(x1, x2, x3)
답변 (1개)
Rik
2020년 6월 10일
You can use something like this:
close all
x1=rand(10,1);
myfun(x1)
x2=rand(10,1);
myfun(x2)
x3=rand(10,1);
myfun(x3)
x4=rand(10,1);
myfun(x4)
function myfun(x)
%write documentation here
x=(x-mean(x))/std(x);
% Creating empirical distribution and plot the results together with normal
% distribution N(0,1)
pd=fitdist(x,'Kernel');
s=(-3:0.01:3);
y=pdf(pd,s);
h=get(gcf,'UserData');
if isempty(h)%initialize
h.C=get(gca,'colororder');
h.counter=0;
pd_=makedist('Normal');
y_=pdf(pd_,s);
plot(s,y_,'-k'),hold on
end
h.counter=h.counter+1;
if h.counter>size(h.C,1),h.counter=1;end
set(gcf,'UserData',h)
C=h.C(h.counter,:);
plot(s,y,'-','Color',C)
end
댓글 수: 4
gjashta
2020년 6월 10일
Rik
2020년 6월 10일
What release are you using?
gjashta
2020년 6월 10일
Rik
2020년 6월 10일
What I meant was: which version of Matlab are you using? I'm using R202a and this code works. So if you are using the same release you must be running the code differently.
If you want to essentially plot the histograms, why not use the histogram function? And if you want to use the actual data, and not the normalized mean, you should probably remove lines like x1=(x1-mean(x1))/std(x1);.
카테고리
도움말 센터 및 File Exchange에서 Exploration and Visualization에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!