In a given number of trials, I need to get the first and last number of rolls whose value is defined by the user.

Hi everyone, I looking for a code to get the first and last number of 1000 rolls whose value was equal to 4. Assume, for 1000 rolls of a pair of dice, find the first and last number of rolls whose value was equal to 4 and whose value lies inbetween 3 and 7. I used to randi function to generate random numbers.

 Accepted Answer

Seelam - you should be able to use find to get the first and last index (roll) whose value equals (for example) four. For example,
rolledNumbers = randi(6,1000,1);
firstRollIs4 = find(rolledNumbers==4,1,'first');
lastRollIs4 = find(rolledNumbers==4,1,'last');
Start with the above and see if you can get the code to do what you wish.

2 Comments

Thank you Geoff Hayes , I tried myself for long time by using 'find(dice=4)' and other syntaxes. Could you explain why we used 4,1 in firstRollIs4 = find(rolledNumbers==4,1,'first');
Can I use this syntax firstRollIs4 = find(dice==7,12,'first'); lastRollIs4 = find(rolledNumbers==7,12,'last');
for finding first and last numbers for value lay between 7 and 12.
Seelam - no, you can't use the syntax that you have shown above. Please read the documentation for find in the link provided in the answer and understand what the inputs are to this function.

Sign in to comment.

More Answers (0)

Categories

Asked:

on 28 Oct 2015

Commented:

on 29 Oct 2015

Community Treasure Hunt

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

Start Hunting!