Creating a matrix with three different values with different probability.

2 次查看(过去 30 天)
I'm trying to generate a matrix with three distinct values with each probability 1/6 and 0 with probability 2/3.
  1. I'm trying to approach this problem by creating a first vector with 25 elements with , second vector with 25 elements with , third vector with 100 elements with 0.
  2. I will use horzcat to create a vector of .
  3. Then I reshuffle the vector using randperm.
  4. Finally, I will use reshape to convert vector into a matrix of .
I would like to know whether this approach is correct. If it is correct, is there any other better approach to solve this problem ?
c1= sqrt(3)*ones(1,25);
c2= -sqrt(3)*ones(1,25);
c3= zeros(1,100);
c = horzcat(c1, c2, c3)
d = c(randperm(length(c)));
T = reshape(d, 10, 15);
Many thanks for this wonderful community.

采纳的回答

Walter Roberson
Walter Roberson 2019-3-26
That approach is appropriate for the case where the exact number of each value is fixed in advance. It is not appropriate when the probabilities are independent of each other, where fractions are statistical rather than exact.
One approach is to take
temp = [sqrt(3), -sqrt(3), 0, 0, 0, 0];
output = temp(randi(length(temp), 10, 15));
This fills statistically rather than with exact fractions.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Random Number Generation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by