# getting an error in this code

조회 수: 4(최근 30일)
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 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 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.
##### 댓글 수: 0표시숨기기 이전 댓글 수: -1

댓글을 달려면 로그인하십시오.

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

댓글을 달려면 로그인하십시오.

### 범주

Find more on Whos in Help Center and File Exchange

R2022a

### Community Treasure Hunt

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

Start Hunting!

Translated by