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 次查看(过去 30 天)
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]));]

采纳的回答

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

更多回答(1 个)

John
John 2016-10-25
Thank you!

类别

Help CenterFile Exchange 中查找有关 Operators and Elementary Operations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by