How to create a random permutation that has specific values in a specific place?
Show older comments
I am trying to get a trial order that randomises between sequences but not within. This code works for my purposes:
temp(1).compound = {'G'};
temp(2).compound = {'R'};
temp(3).compound = {'A'};
temp(4).compound = {'RA'};
temp(5).compound = {'B'};
temp(6).compound = {'B'};
temp(7).compound = {'G'};
temp(8).compound = {'B'};
temp(9).compound = {'G'};
temp(10).compound = {'R'};
temp(11).compound = {'RA'};
sequence1(1) = 3;
sequence2(1)=1;
sequence2(2)=7;
sequence2(3)=9;
sequence3(1)=2;
sequence3(2)=4;
sequence3(3)=10;
sequence3(4)=11;
sequence4(1)=5;
sequence5(1)=6;
sequence5(2)=8;
sequenece = {sequence1, sequence2, sequence3, sequence4, sequence5};
ind = randperm(numel(sequenece));
shuffled_sequenece = [sequenece{ind}];
for i = 1:length(temp)
TRIAL(i)=temp(shuffled_sequenece(i));
end
I need all sequences to be in TRIAL but I want sequence1 or sequence4 to be randomly fixed at trial number 6. How could I do this?
2 Comments
@William Nicholson: using numbered variables is a sign that you are doing something wrong. Trying to access variable names dynamically is one way that beginners force themselves into writing slow, complex, buggy code that is hard to debug. Read this to know more:
Your code would be simpler and much more efficient if you just used indexing.
QUESTION: do the index vectors within sequence need to be kept together, or can they be split by element 6? For example, if the index vectors of sequence sequence are arranged like this:
[[1,7,9], [2,4,10,11], ...] ->
[ 1, 7, 9, 2, 4, 10, 11, ... ]
^^ sixth element
then the sixth element is 10: what do you want done to this sequence: split it apart, something like this:
[1,7,9,2,4,5,10,11,...]
^^^^ split and insert one of the required indices?
William Nicholson
on 24 Aug 2018
Accepted Answer
More Answers (1)
dpb
on 22 Aug 2018
Writing multiple sequentially-numbered variables is a bad idea in general but won't try to get into fixing that here...but if were indexed or struct fields or other ways to implement wouldn't have to rewrite code lines...
if rand<0.5
sequence = {sequence1, sequence2, sequence3, sequence4, sequence5};
else
sequence = {sequence4, sequence2, sequence3, sequence1, sequence5};
end
if I get your intent.
3 Comments
William Nicholson
on 22 Aug 2018
dpb
on 22 Aug 2018
Yeah, but as noted you need to store the data in arrays or named fields in a struct in order to address it dynamically rather than using sequentially-named variables.
William Nicholson
on 24 Aug 2018
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!