필터 지우기
필터 지우기

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에서 Build Deep Neural Networks에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by