Create 9x9 matrix with RANDI between [0,2] with each 0,1, and 2 repeating three times each per column.
조회 수: 18 (최근 30일)
이전 댓글 표시
I'm trying to create a 9x9 matrix with random number generation from 0 to 2 using RANDI:
X = randi([0,2], 9,9)
However, I want to force each of the 9 columns to show an equal distribution of 0's, 1's, and 2's (3 x of each number per column). I'm having difficulty finding a loop that can do this. Any help would be great (very new to this type of MATLAB work).
Thanks.
댓글 수: 0
채택된 답변
Abdolkarim Mohammadi
2020년 9월 23일
편집: Abdolkarim Mohammadi
2020년 9월 23일
You can first create a vector with the values you need:
Reference = repelem (0:2,3)';
Then you initialize the 9x9 matrix with zeros(), and finally fill in the columns with random permutations of Reference using randperm().
댓글 수: 6
Abdolkarim Mohammadi
2020년 9월 23일
편집: Abdolkarim Mohammadi
2020년 9월 23일
Yes. But the number of rows should not change. For example: zeros (9,4). Otherwise, you should update Reference have the same number of elements as the number of rows in M.
추가 답변 (1개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!