Equal Sized random assortment, using randi?

2 Ansichten (letzte 30 Tage)
Chris Keightley
Chris Keightley am 2 Mai 2021
Kommentiert: Chad Greene am 3 Mai 2021
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;

Akzeptierte Antwort

Image Analyst
Image Analyst am 2 Mai 2021
Chris: first you need to create a column vector with the desired number of 1s and 2s in the column. Then you need to use randperm() to scramble the order and stick it in your output array:
num1s = 10; % Whatever you want
num2s = 10; % Whatever you want
% Make a column vector with the specified number of 1s and 2s in it.
unscrambled = [ones(num1s, 1); 2 * ones(num2s, 1)]
rows = length(unscrambled);
columns = 16; % Whatever you want
output = zeros(rows, columns);
% Scramble and place into the output matrix.
for col = 1 : columns
sortOrder = randperm(rows);
output(:, col) = unscrambled(sortOrder);
end
output % See it in the command window.
  5 Kommentare
Image Analyst
Image Analyst am 2 Mai 2021
Bearbeitet: Image Analyst am 2 Mai 2021
Chad, I don't understand what you're saying. If I change it to have
num1s = 4; % Whatever you want
num2s = 3; % Whatever you want
% Make a column vector with the specified number of 1s and 2s in it.
unscrambled = [ones(num1s, 1); 2 * ones(num2s, 1)]
rows = length(unscrambled);
columns = 9; % Whatever you want
output = zeros(rows, columns);
% Scramble and place into the output matrix.
for col = 1 : columns
sortOrder = randperm(rows);
output(:, col) = unscrambled(sortOrder);
end
output % See it in the command window.
so that 4+3 < 9, it still works just fine with the specified number of 1s and 2s in each column.
output =
2 1 2 2 1 1 1 1 1
1 1 2 1 2 1 1 1 1
1 2 1 1 1 2 1 1 2
2 1 1 1 1 1 2 2 1
1 1 1 2 2 1 1 2 1
2 2 2 1 1 2 2 2 2
1 2 1 2 2 2 2 1 2
And since unscrambled is a column vector, length(unscrambled) is the same as size(unscrambled, 1). How would there be an error?
Chad Greene
Chad Greene am 3 Mai 2021
Oh heck, you're right @Image Analyst. I misread, thinking that unscrambled was a matrix.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Chad Greene
Chad Greene am 2 Mai 2021
Bearbeitet: Chad Greene am 2 Mai 2021
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)
  1 Kommentar
Chris Keightley
Chris Keightley am 2 Mai 2021
Wow, truly mind-blowing, thanks a lot Chad, love the pictorial demonstration as well.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Psychology finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by