# Matlab help

Announcements
#1
how do i do this

Exercise 2.9 (Hard) A board game is played with two dice. One of the dice has its faces numbered from 1 to 6, the other dice has a different colour in each of its faces: red, blue, green, yellow, orange and purple. Write a MATLAB function named throwdice.m that simulates the two dice being thrown and returns one number and a colour. Hint: you can use the MATLAB built-in function randi.
0
2 years ago
#2
You could do it with a bunch of if-else statements. Call randi to generate two numbers between 1 and 6, and then for one of the die, assign a colour to each number using the if elses, then just return the number of the die and a colour.

This is a basic programming task? What precisely do you need help with.
0
#3
(Original post by vicvic38)
You could do it with a bunch of if-else statements. Call randi to generate two numbers between 1 and 6, and then for one of the die, assign a colour to each number using the if elses, then just return the number of the die and a colour.

This is a basic programming task? What precisely do you need help with.
ive got it so i can get a random number but i cant get it to chose a random colour, how do i exactly do that, and also this is my first time using matlab as well
0
2 years ago
#4
(Original post by Haamid)
ive got it so i can get a random number but i cant get it to chose a random colour, how do i exactly do that
You use if else statements to assign the result of Randi to a colour. Something like the pseudocode here:
IF die2 = 1 THEN:
colour = red
ELSEIF die2 = 2 THEN:
colour = blue
ELSEIF die2 = 3 THEN:
colour = green
ELSEIF die2 = 4 THEN:
colour = yellow
ELSEIF die2 = 5 THEN:
colour = orange
ELSE:
colour = purple
END

I'm sure you could do it with a matrix? You could also define a vector whose elements were the colours, and select them using the Randi result:
Colours = [red, blue, green, yellow, orange, purple]
colour = Colours(die2)
0
2 years ago
#5
I would set up an array for the colours, e.g. colours = [red, blue, green], that way you don't have to have a bunch of if statements
0
#6
Finally did it, with a simple version. this is the input script

function T=throwdice()
%Rolling a dice with numbers on one and colours on the other.
colours = (["red","blue","green","purple","yellow","orange"]);
die2 = randi(6);
colour = colours(die2);

disp(['The random number is ', num2str(randi(6,1)), ' and the random color is ', num2str(colour)])

final reuslt is
>> throwdice()
The random number is 2 and the random color is red
>> throwdice()
The random number is 1 and the random color is green
>> throwdice()
The random number is 3 and the random color is purple

0
X

new posts Back
to top
Latest

### Oops, nobody has postedin the last few hours.

Why not re-start the conversation?

see more

### Poll

Join the discussion

Talking to current university students (18)
19.15%
Talking to peers going through the same thing (32)
34.04%
Speaking to student ambassadors from the universities (5)
5.32%
Speaking to staff members from universities (2)
2.13%
Using the personal statement builder, library or helper service (9)
9.57%
19.15%
Learning about/speaking to Student Finance England (4)
4.26%
Something else (tell us in the thread) (6)
6.38%