MATLAB Answers

Brave A
0

Difference and probability in plot

Asked by Brave A
on 3 Sep 2019
Latest activity Commented on by Brave A
on 4 Sep 2019
Accepted Answer by KSSV
Hello I tried to plot this but it's show me "Vectors must be the same length.
Error in (line 17)
plot(difference,probability,'b--'"
would you please help me to fix the problem?
thanks in advance.
difference=[];
probability=[];
for diff=-5:1:5
sum=0;
for g=1:6
for r=1:6
if(g-r==diff)
sum=sum+(1/36);
end
end
end
differnce=[difference diff];
probability=[probability sum];
end
figure
plot(difference,probability,'b--')
xlabel('D');
ylabel('P');
axis([-5 5 0 .2]);

  0 Comments

Sign in to comment.

Tags

1 Answer

Answer by KSSV
on 3 Sep 2019
Edited by KSSV
on 3 Sep 2019
 Accepted Answer

thedifference=-5:1:5 ;
N = length(thedifference) ;
probability=zeros(N,1) ;
for i = 1:N
thediff = thedifference(i) ;
thesum=0;
for g=1:6
for r=1:6
if(g-r==thediff)
thesum=thesum+(1/36);
end
end
end
probability(i) = thesum;
end
figure
plot(thedifference,probability,'b--')
xlabel('D');
ylabel('P');
axis([-5 5 0 .2]);

  5 Comments

thanks it's work.
but how to make x axis gives all values from -5 to 0 to 5 not only those values, I need all values in between.
KSSV
on 3 Sep 2019
Give your desired values in the line:
thedifference=-5:1:5 ;

Sign in to comment.