How to create a matrix with user-defined distribution?

I'm trying to create a matrix composed of numbers 1,2 and 3 with user-defined distribution of these numbers. User-defined distribution in this case means that sum of numbers 1, 2 and 3 is the same for all matrix columns but varies from row to row. Example of such matrix:
I want 6 1s, 3 2s, and 4 3s in each column, but also 9 1s, 4 2s and 3 3s in first row; 6 1s, 6 2s and 4 3s in second row and so on.
What have I tried already:
  1. Fill the matrix with randomly generated integers from 1 to 3, then compute the sums and find the differences between computed sums and those predefined by user. Set up the tolerance (let's say "tol < 1") and create the matrix again and again until the resulting differences meet the tolerance condition. This solution doesn't work as the number of iterations is way too big.
  2. Start with filling all the matrix columns the same way manually so that I would get the desired distribution. Then shuffle elements in the matrix. However, the same problem as in the first case occurs.
  3. The same thing described in the second point, but in this case take a look at resulting differences separately. If the tolerance condition is fulfilled in any of the rows, then I would preserve the row and shuffle elements in all remaining ones. My idea was to continue doing so row-by-row but, unfortunately, this solution doesn't work either. It works nicely for the majority of rows, but when there are two rows left, it breaks down as the solution does not converge. I can never get the right differences from the combination of numbers left in those last two rows.
Now I got stuck. Any suggestions on how to continue?

1 个评论

HUH? You said the SUM of the numbers is the same. But apparently what you wanted is a fixed number of 1s, 2s, and 3s in each column. That is very different, and also trivial.
Just generate a matrix with six 1s, three 2s, and four 3s in every column. Then sort each column randomly and independently. Nothing wrong about a loop there.

请先登录,再进行评论。

回答(1 个)

Since the distribution of 1, 2 and 3 is fixed per column and it's just the position that varies, simply use randperm to generate as many columns as necessary:
column = repelem([1;2;3], [6 3 4]); %column vector with 6 1s, 3 2s, and 4 3s
numcolumn = 16;
m = cell2mat(arrayfun(@(~) column(randperm(numel(column))), 1:numcolum, 'UniformOutput', false))

3 个评论

Not only that the distribution is fixed per column, but it is fixed per rows as well. If I use the matrix inserted above, I want 6 1s, 3 2s, and 4 3s in each column, but also 9 1s, 4 2s and 3 3s in first row; 6 1s, 6 2s and 4 3s in second row and so on.
I made four changes in the matrix but I'm still getting same sums, so there are definitely multiple solutions.
If this is a thing people do, for example like Latin Squares, then there should be an algorithm out there for doing it. What is this concept called, and why do you need it? Do you know of anyplace where the algorithm is explained?

请先登录,再进行评论。

类别

在 帮助中心 和 File Exchange 中查找有关 Random Number Generation 的更多信息

提问:

2016-12-11

Community Treasure Hunt

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

Start Hunting!

Translated by