One approach would be to construct the set of all possible sequences of length perhaps 10.
seq10 = char(dec2base(0:59048,3) + 1);
k = any(diff(seq10,[],2) == 0,2);
seq10(k,:) = [];
This produces a set of 1536 sequences of length 10.
size(seq10)
ans =
1536 10
seq10(1:100:end,:)
ans =
16×10 char array
'1212121212'
'1213212312'
'1232123121'
'1312313212'
'1321231212'
'1323231312'
'2123132121'
'2131323212'
'2312312121'
'2321212312'
'2323213121'
'3123123212'
'3131321212'
'3212131312'
'3213232121'
'3232132312'
some of which are shown above. Of course, this is still too broad of a set, as it includes sequences that are illegal. I next need to exclude sequences that do not contain a 3 in a subsequence [1 ? 1] or [2 ? 2]. Therefore, I just need to exclude any strings in this set that contain either '121' or '212' as a substring.
n10 = size(seq10,1);
k = false(n10,1);
for i = 1:n10
k(i) = ~isempty(strfind(seq10(i,:),'121')) | ~isempty(strfind(seq10(i,:),'212'));
end
seq10(k,:) = [];
n10 = size(seq10,1)
n10 =
552
Too early in the morning for me to think of how to use regexp to do that last operation. Sorry. There are 552 such sequences of length 10. The problem is, this scheme becomes intractable when sequences of length say 15 or 20 must be constructed. Constructing all possible sequences of a longer length would be difficult.
Therefore, consider a different approach, where I'll construct the set of all admissable sequences recursively.
Suppose a given sequence ends with the subsequence: '12'. The next element canot be 1, since then we have the subsequence '121'. It cannot be 2, since then we swould have '122'. This means the next element must ALWAYS be 3.
Similarly, suppose the sequence ends with '13'. Now the legal continuations are always either '132' or '131'.
finallength = 30;
seqs2 = ['12';'13';'21';'23';'31';'32'];
for n = (size(allseqs,2)+1):finallength
newseqs = '';
for i = 1:6
k = ismember(allseqs(:,[end-1,end]),seqs2(i,:),'rows');
nk = sum(k);
switch i
case {1 3}
newseqs = [newseqs;[allseqs(k,:),repmat('3',nk,1)]];
case {2 4}
newseqs = [newseqs;[allseqs(k,:),repmat('1',nk,1)];[allseqs(k,:),repmat('2',nk,1)]];
case 5
newseqs = [newseqs;[allseqs(k,:),repmat('2',nk,1)];[allseqs(k,:),repmat('3',nk,1)]];
case 6
newseqs = [newseqs;[allseqs(k,:),repmat('1',nk,1)];[allseqs(k,:),repmat('3',nk,1)]];
end
end
allseqs = newseqs;
end
size(allseqs)
The code above, with finallength as successively [5,10,15,20,25,30], yields the number of all possible sequences as respectively [32, 552, 9568, 165888, 2876160, 49866752]. I cannot go much beyond that point.
Of those roughly 50 million possible sequences of length 30. We could choose one at random.
seq = allseqs(randi(n30),:)
seq =
'232323132321323132131232131231'
accumarray(seq' - '0',1)
ans =
8
10
12
So in this initial sequence of length 30, we found 8 ones, 10 twos, and 12 threes. We could now append one of the sequences from allseqs, as long as it starts in a way that is compatible with the terminating characters of seq, and as long as the total number of 1,2,3 elements are not more than the target of 46,40,45.
3 Comments
Direct link to this comment
https://in.mathworks.com/matlabcentral/answers/569655-how-can-i-create-a-vector-with-constrains-on-matlab#comment_947560
Direct link to this comment
https://in.mathworks.com/matlabcentral/answers/569655-how-can-i-create-a-vector-with-constrains-on-matlab#comment_947560
Direct link to this comment
https://in.mathworks.com/matlabcentral/answers/569655-how-can-i-create-a-vector-with-constrains-on-matlab#comment_947563
Direct link to this comment
https://in.mathworks.com/matlabcentral/answers/569655-how-can-i-create-a-vector-with-constrains-on-matlab#comment_947563
Direct link to this comment
https://in.mathworks.com/matlabcentral/answers/569655-how-can-i-create-a-vector-with-constrains-on-matlab#comment_947572
Direct link to this comment
https://in.mathworks.com/matlabcentral/answers/569655-how-can-i-create-a-vector-with-constrains-on-matlab#comment_947572
Sign in to comment.