Borrar filtros
Borrar filtros

Trying to create an m x n (n=2 columns) matrix where n is incremented in integers starting with 1 and the second column outputs a random integer from 1 to 6 as if 1 die were being thrown n times.

1 visualización (últimos 30 días)
Is there a better was to achieve the results of the following code without having to re-type the row and column elements 5 times?
dieRolls5 = [1,(randi ([1,6]));2,(randi ([1,6]));3,(randi ([1,6]));4,(randi ([1,6]));5,(randi ([1,6]));]
The results are correct but one must do a lot of manual work to simulate the results of a different number of throws of the die.
One might want to create a matrix with a different number of throws of the die; e.g., 8 throws. Having to type the following code is obviously not a good solution.
diceRoll8 = [1,(randi ([1,6]));2,(randi ([1,6]));3,(randi ([1,6]));4,(randi ([1,6]));5,(randi ([1,6]));6,(randi ([1,6]));7,(randi ([1,6]));8,(randi ([1,6]));]

Respuesta aceptada

Walter Roberson
Walter Roberson el 25 de Oct. de 2016
N = 8;
dieRolls = [(1:N).', randi(6,N,1)];

Más respuestas (1)

John
John el 25 de Oct. de 2016
Thank you!

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by