# getting an error in this code

3 views (last 30 days)
Bobby on 29 Apr 2022
Answered: Mathieu NOE on 29 Apr 2022
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?
##### 0 CommentsShow -2 older commentsHide -2 older comments

Sign in to comment.

### Accepted Answer

Mahmoud Ashraf on 29 Apr 2022
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
##### 0 CommentsShow -2 older commentsHide -2 older comments

Sign in to comment.

### More Answers (2)

Voss on 29 Apr 2022
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 CommentsShow -2 older commentsHide -2 older comments

Sign in to comment.

Mathieu NOE on 29 Apr 2022
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
##### 0 CommentsShow -2 older commentsHide -2 older comments

Sign in to comment.

### Categories

Find more on Frequently-used Algorithms 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!