How do I randomly generate numbers with a certain amount of probability?

2 次查看(过去 30 天)
Hi there,
Brand new to programming so sorry if this is a dumb question.
I have two folders with twenty-four images each, one folder contains emotional images and the other folder contains non-emotional images. The images are to be displayed on the left and right side of the screen under four conditions:
Non-emotional on the left - Emotional on the right for 6 trials
Emotional on the left - Non-emotional on the right for 6 trials
Non-emotional on the left - Non-emotional on the right for 6 trials
Emotional on the left - Emotional on the right for 6 trials
I figure I can do something like this using if statements, for loops, and randomly generating 4 different integers. The problem is if I do this by generating a random integer from 1-4, I want to make sure no integer is generated more than 6 times, because that would mean that the condition occurs for more than 6 trials. How can I do this?
Alternatively, is there a better way to do what I want to do?
Thanks in advance,

回答(1 个)

John D'Errico
John D'Errico 2019-5-12
As is often the case, I would suggest you are overthinking this.
You have a sequence of 24 pairs of images to be shown. There will be 6 trials each for each combination. (At each trial, I assume you will randomly choose a pair of images from each folder. But that is not relevant at this point.)
So, consider what this matrix would indicate, assuming that a 1 indicates a non-emotional picture, and 2 indicates a choice from the set of emotional images.
trials = repmat([1 2;2 1;1 1;2 2],6,1);
trials = trials(randperm(24),:)
trials =
2 2
2 1
2 1
1 2
2 2
2 1
1 2
...
Each combination occurs exactly 6 times, in randomly permuted order.

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by