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 Center および File ExchangeFrequently-used Algorithms についてさらに検索

タグ

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by