getting an error in this code

조회 수: 6 (최근 30일)
Bobby
Bobby 2022년 4월 29일
답변: Mathieu NOE 2022년 4월 29일
clc
clear
%shipcostpmf.m
sx = (1:8);
px = [0.15*ones(4,1), 0.1*ones(4,1)];
gx = (sx<=5).*(105*sx-5*(sx.^2)) + ((sx>5).*500);
sy = unique(gx);
fprintf('sy =\n');
disp(sy);
py = finitepmf(gx,px,sy);
not sure why my py is getting an error because it says Unrecognized function or variable 'finitepmf'. but finitepmf should be a command?

채택된 답변

Mahmoud Ashraf
Mahmoud Ashraf 2022년 4월 29일
you should define funstion of finitrepmf
as shown in this code
clc
clear
%shipcostpmf.m
sx = (1:8);
px = [0.15*ones(4,1), 0.1*ones(4,1)];
gx = (sx<=5).*(105*sx-5*(sx.^2)) + ((sx>5).*500);
sy = unique(gx);
fprintf('sy =\n');
disp(sy);
py = finitepmf(gx,px,sy);
function pmf=finitepmf(sx,px,x)
% finite random variable X:
% vector sx of sample space
% elements {sx(1),sx(2), ...}
% vector px of probabilities
% px(i)=P[X=sx(i)]
% Output is the vector
% pmf: pmf(i)=P[X=x(i)]
pmf=zeros(size(x(:)));
for i=1:length(x)
pmf(i)= sum(px(find(sx==x(i))));
end
end

추가 답변 (2개)

Voss
Voss 2022년 4월 29일
Maybe finitepmf should be defined as follows:
function pmf=finitepmf(sx,px,x)
% finite random variable X:
% vector sx of sample space
% elements {sx(1),sx(2), ...}
% vector px of probabilities
% px(i)=P[X=sx(i)]
% Output is the vector
% pmf: pmf(i)=P[X=x(i)]
pmf=zeros(size(x(:)));
for i=1:length(x)
pmf(i)= sum(px(find(sx==x(i))));
end
end
which was found here:
If that seems right and you haven't done so already, put that code into an m-file called finitepmf.m. If you have done that already, then make sure that finitepmf.m is on your MATLAB search path.

Mathieu NOE
Mathieu NOE 2022년 4월 29일
hi
here you are
sx = (1:8);
px = [0.15*ones(4,1), 0.1*ones(4,1)];
gx = (sx<=5).*(105*sx-5*(sx.^2)) + ((sx>5).*500);
sy = unique(gx);
fprintf('sy =\n');
disp(sy);
py = finitepmf(gx,px,sy)
function pmf=finitepmf(sx,px,x)
% finite random variable X:
% vector sx of sample space
% elements {sx(1),sx(2), ...}
% vector px of probabilities
% px(i)=P[X=sx(i)]
% Output is the vector
% pmf: pmf(i)=P[X=x(i)]
pmf=zeros(size(x(:)));
for i=1:length(x)
pmf(i)= sum(px(sx==x(i)));
end
end

카테고리

Help CenterFile Exchange에서 Frequently-used Algorithms에 대해 자세히 알아보기

태그

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by