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]);

 채택된 답변

KSSV
KSSV 2019년 9월 3일
편집: KSSV 2019년 9월 3일

0 개 추천

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

madhan ravi
madhan ravi 2019년 9월 3일
편집: madhan ravi 2019년 9월 3일
Naming a variable sum and diff is a bad idea.
KSSV
KSSV 2019년 9월 3일
Yes...you are right.......changed it. @Rana, never name variables on the names of inbuilt functions.
Brave A
Brave A 2019년 9월 3일
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.
Give your desired values in the line:
thedifference=-5:1:5 ;

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

추가 답변 (0개)

태그

질문:

2019년 9월 3일

댓글:

2019년 9월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by