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

2 ビュー (過去 30 日間)
Robert Newman
Robert Newman 2019 年 5 月 12 日
回答済み: John D'Errico 2019 年 5 月 12 日
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 件のコメント
Image Analyst
Image Analyst 2019 年 5 月 12 日
Have you tried dir(), randperm(), and imshow()?
Have you seen the FAQ?

サインインしてコメントする。

回答 (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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by