http://www.mathworks.com/matlabcentral/newsreader/view_thread/315742
MATLAB Central Newsreader  Randomising image stimuli
Feed for thread: Randomising image stimuli
enus
©19942014 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Fri, 06 Jan 2012 10:56:08 +0000
Randomising image stimuli
http://www.mathworks.com/matlabcentral/newsreader/view_thread/315742#863009
Norm
I'm new to Matlab and need to randomise some stimuli for an experiment. I have 25 images which need to be shown in two alternating conditions without the same image being shown twice in a row. What is the simplist way to do this?

Sat, 07 Jan 2012 02:20:08 +0000
Re: Randomising image stimuli
http://www.mathworks.com/matlabcentral/newsreader/view_thread/315742#863070
Roger Stafford
"Norm" wrote in message <je6k08$pbo$1@newscl01ah.mathworks.com>...<br>
> I'm new to Matlab and need to randomise some stimuli for an experiment. I have 25 images which need to be shown in two alternating conditions without the same image being shown twice in a row. What is the simplist way to do this?<br>
        <br>
This may not be the simplest way to do this but at least it gives an answer.<br>
<br>
n = 25;<br>
p1 = 1:n+1;<br>
p2 = zeros(1,n);<br>
m = 0;<br>
while m == 0<br>
b1 = [true(1,n),false];<br>
b2 = [false,false,true(1,n1)];<br>
for k = 1:n<br>
b = b1 & b2;<br>
m = sum(b);<br>
if m==0, break, end<br>
a = p1(b);<br>
r = a(ceil(m*rand));<br>
p2(k) = r;<br>
b1(r) = false;<br>
b2 = b2([n+1,1:n]);<br>
end<br>
end<br>
q = randperm(n);<br>
p1 = q(p1(1:n));<br>
p2 = q(p2);<br>
<br>
The indices in p1 and p2 each represent a distinct permutation of the 25 images with the p1 being those with the first "condition" and p2 the second. The indices in p1 and p2 are to be used alternately with the first element of p1 first, the first element of p2 second, the second element of p1 third, etc. until all 25 images have been used with each condition.<br>
<br>
Probably you will want to write something like:<br>
<br>
p = reshape([p1;p2],1,[]);<br>
<br>
where p gives 50 successive image indices, each of the 25 used twice, with the condition understood to alternate. No consecutive pairs should be alike.<br>
<br>
I was unable to think of a way to make all different possible arrangements have the same theoretical probabilities, nor to insure that every first try would always succeed. However the above whileloop only fails on its first trip about once every 20 or 30 times, and the probability differences are likely too subtle to be noticed unless n is very much smaller than 25, so perhaps you will be content with this.<br>
<br>
Roger Stafford

Sat, 07 Jan 2012 18:03:07 +0000
Re: Randomising image stimuli
http://www.mathworks.com/matlabcentral/newsreader/view_thread/315742#863107
Roger Stafford
"Roger Stafford" wrote in message <je8a4o$p5m$1@newscl01ah.mathworks.com>...<br>
> "Norm" wrote in message <je6k08$pbo$1@newscl01ah.mathworks.com>...<br>
> > I'm new to Matlab and need to randomise some stimuli for an experiment. I have 25 images which need to be shown in two alternating conditions without the same image being shown twice in a row. What is the simplist way to do this?<br>
>         <br>
> This may not be the simplest way to do this but at least it gives an answer.<br>
> <br>
> n = 25;<br>
> p1 = 1:n+1;<br>
> p2 = zeros(1,n);<br>
> m = 0;<br>
> while m == 0<br>
> b1 = [true(1,n),false];<br>
> b2 = [false,false,true(1,n1)];<br>
> for k = 1:n<br>
> b = b1 & b2;<br>
> m = sum(b);<br>
> if m==0, break, end<br>
> a = p1(b);<br>
> r = a(ceil(m*rand));<br>
> p2(k) = r;<br>
> b1(r) = false;<br>
> b2 = b2([n+1,1:n]);<br>
> end<br>
> end<br>
> q = randperm(n);<br>
> p1 = q(p1(1:n));<br>
> p2 = q(p2);<br>
> <br>
> The indices in p1 and p2 each represent a distinct permutation of the 25 images with the p1 being those with the first "condition" and p2 the second. The indices in p1 and p2 are to be used alternately with the first element of p1 first, the first element of p2 second, the second element of p1 third, etc. until all 25 images have been used with each condition.<br>
> <br>
> Probably you will want to write something like:<br>
> <br>
> p = reshape([p1;p2],1,[]);<br>
> <br>
> where p gives 50 successive image indices, each of the 25 used twice, with the condition understood to alternate. No consecutive pairs should be alike.<br>
> <br>
> I was unable to think of a way to make all different possible arrangements have the same theoretical probabilities, nor to insure that every first try would always succeed. However the above whileloop only fails on its first trip about once every 20 or 30 times, and the probability differences are likely too subtle to be noticed unless n is very much smaller than 25, so perhaps you will be content with this.<br>
> <br>
> Roger Stafford<br>
       <br>
I am chagrined to find that the following more crude method of trying repeatedly to pair one random permutation with another and rejecting the failures should be considerable faster statistically in matlab (at least for n = 25) than my earlier more careful one in this thread, even though this one requires many more tries through the whileloop on average to achieve success. You would be advised to use this more crude method. Also it is guaranteed to choose permutation pairs equally among all those possible.<br>
<br>
n = 25;<br>
p1 = randperm(n);<br>
b = true;<br>
while b % Keep trying until an acceptable p2 shows up<br>
p2 = randperm(n);<br>
b = any(p1==p2)  any(p1(2:n)=p2(1:n1)); % Rejection criterion<br>
end<br>
p = reshape([p1;p2],1,[]);<br>
<br>
Roger Stafford