Randomise List with Repetitions

3 Ansichten (letzte 30 Tage)
Tutasaurus
Tutasaurus am 2 Feb. 2018
Kommentiert: Star Strider am 9 Feb. 2018
I managed to create a list of images (images, 80% one image; 20% another one). These images are to be randomized, which is fine, except for the fact that:
  • I need there to be at least one of the "20% Images" in every 12 rows,
  • There can be no more than 3 "20% Images" in every 12 rows.
Im able to randomize the list however can't figure out how to set these randomization restrictions I want. This is for an oddball experiment. Any help is appreciated.

Antworten (2)

Star Strider
Star Strider am 2 Feb. 2018
One approach:
Ncols = 10; % Columns In Matrix
v = 8*ones(12,Ncols); % Preallocate
p20 = randi(3, 1, Ncols); % Number Of Rows Allocated To ‘20%’ In Each Column
for k1 = 1:Ncols
idxv = randperm(12,p20(k1)); % Choose Rows For ‘20%’
v(idxv,k1) = 2; % Define Matrix
end
v =
8 2 8 2 8 8 8 2 8 8
2 8 8 8 2 8 8 2 8 8
8 2 8 8 2 8 2 8 8 8
2 8 2 8 2 8 8 8 8 8
8 8 8 8 8 2 8 8 8 8
8 8 8 8 8 8 8 8 8 8
8 8 8 8 8 8 8 8 2 2
2 8 8 2 8 8 2 8 8 2
8 8 8 8 8 8 8 8 2 8
8 8 8 2 8 8 8 8 2 8
8 8 8 8 8 8 8 8 8 8
8 8 2 8 8 8 8 8 8 8
The ‘80%’ images are any corresponding to an ‘8’, and the ‘20%’ to a 2.
  4 Kommentare
Tutasaurus
Tutasaurus am 9 Feb. 2018
Ok thanks! I think I can use this and append the file names depending on whether its an "8" or "2" in a loop as well. Will give it a try.
Star Strider
Star Strider am 9 Feb. 2018
My pleasure!

Melden Sie sich an, um zu kommentieren.


Roger Stafford
Roger Stafford am 9 Feb. 2018
Without your restriction as to the number of 20% images, you would be asking for a binary distribution for each set of 12 images. In that distribution the probabilities for the three cases you are restricted to are:
p1 = 12/1*.2*.8^11 % <-- One 20% case
p2 = 12*11/1/2*.2^2*.8^10 % <-- Two 20% cases
p3 = 12*11*10/1/2/3*.2^3*.8^9 % <-- Three 20% cases
Given your restriction, these normalize so as to add to one by doing this:
s = p1+p2+p3
p1 = p1/s
p2 = p2/s
p3 = p3/s
These latter are respectively about 28%, 39%, and 33%. The only way Star’s method deviates from this is that he assigned equal probabilities for these three cases in his code. If you want a more accurate realization as a “restricted” binary distribution, you would have to replace the
p20 = randi(3, 1, Ncols);
step in Star’s code with
r = rand(1,Ncols);
p20 = sum([(p1<=r);(p1+p2<=r)],1)+1;
where the above normalized p1 and p2 are used. The number p20 is still either 1, 2, or 3 but with probabilities p1, p2, and p3 respectively, instead of being equal.

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by