Equal Sized random assortment, using randi?
显示 更早的评论
Hello everyone,
I have used the following code to generate a matrix of random integers (1's and 2's). However, I am finding myself with an unequal amount of random integers in each column (e.g., eleven "1's" and eight "2's" in some columns). I would like to know how I could get a fixed amount of equally sized conditions using this function. In where, I could get ten "1's" and ten "2's" equally spread among all six columns, that still maintains a random order. Am I using the right function (randi) to do accomplish this? Please let me know if I am unclear with my question.
Kind regards,
T = [ ];
ii=1;
while ii<=20;
T(ii,:)= randi(2,1,8);
ii=ii+1;
end;
采纳的回答
更多回答(1 个)
This would be one way to define how many ones and how many twos in each column before randomizing them:
N_cols = 8; % number of columns
N_ones = 10; % number of ones in each column
N_twos = 10; % number of twos in each column
M = [ones(N_ones,N_cols);2*ones(N_twos,N_cols)];
imagesc(M)
% Shuffle the order of each column:
for k = 1:N_cols
M(:,k) = M(randperm(N_ones+N_twos),k);
end
imagesc(M)
类别
在 帮助中心 和 File Exchange 中查找有关 Psychology 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

