I need to make a dice with riged odds and plot the results in a bar chart. The dice and the riged odds part gives me the result im seeking, but i dont know how to convert these anwsers into a bar chart seeming my x- values keeps on changing value.
clc, clear all
for index=(1:1000)
x=randi([1,13]);
if x==1
x=5
elseif 2<=x && x<=5
x=randi([1,2])
elseif 6<=x && x<=7
x=4
elseif 8<=x && x<=9
x=6
elseif 10<=x && x<=13
x=3
end
end
Thanks

 채택된 답변

Star Strider
Star Strider 2020년 2월 3일

1 개 추천

You might find the histogram function useful.

댓글 수: 6

Daniel Hodgson
Daniel Hodgson 2020년 2월 3일
The histogram function only seems to take one input in my case seeming it only reads the final 'x' in my code. is there a way to collect all x-values and lets say place them in a matrix?
Apparently you are only rolling the die three times.
Consider naming the result of the die throws something other than ‘x’, so you have one vector ‘x’ of saved results, and a separate variable for the intermediate results, then subscript them as:
x(index) = ...;
wherever you assign them.
Daniel Hodgson
Daniel Hodgson 2020년 2월 3일
the goal is to simulate this 1000, sry for being unclear. Therefore i tried to make it simpler with on three rolls
Star Strider
Star Strider 2020년 2월 3일
O.K. No worries.
Since this is obviously homework, we are constrained with the help we can offer (by tradition). I would store the results of the die throws that you want to save as the output of your loop in a vector (using ‘index’ or whatever you choose as a subscript), then use histogram on that vector. The histogram function will coount the number of each die throw result, and present it as a bar chart.
Daniel Hodgson
Daniel Hodgson 2020년 2월 3일
THANK YOU SO MUCH <3
This is my code, there is probaly an easier way to solve this problem. But who cares!
clc, clear all
a=1;
A=[1: a];
for i=(1:1000)
a=0
a=1+a;
x=randi([1,13]);
if x==1
x=5
elseif 2<=x && x<=5
x=randi([1,2])
elseif 6<=x && x<=7
x=4
elseif 8<=x && x<=9
x=6
elseif 10<=x && x<=13
x=3
end
A(i)=x;
a=a+1;
end
histogram(A)
Star Strider
Star Strider 2020년 2월 3일
As always, my pleasure!

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

추가 답변 (2개)

Dave
Dave 2020년 2월 3일

0 개 추천

Consider creating an empty matrix prior to the 'for' statement. Then write the results to that matrix.

댓글 수: 1

Daniel Hodgson
Daniel Hodgson 2020년 2월 3일
I have now updated by problem to 1000 rolls ofthe dice. Is there a way for matlab to put these values into the matrix and if so how?

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

Dave
Dave 2020년 2월 3일

0 개 추천

If you haven't already solved this, search for something like "creating arrays with loops". That should give you some examples on how to proceed.

카테고리

도움말 센터File Exchange에서 Data Distribution Plots에 대해 자세히 알아보기

태그

질문:

2020년 2월 3일

댓글:

2020년 2월 3일

Community Treasure Hunt

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

Start Hunting!

Translated by