Generate partial strings which have predefined minimum lengths (Matlab) -
i have initial string init={abcdefgh}. how can generate 100 partial strings (randomly) init string have these conditions:
a pre-defined minimum lengths. order of elements in each partial string should 'a' 'z'. no repeated characters in each partial strings
the expected output should follows: 100 partial strings, minimum length of each partial string 5
output = {'bcegh';'acefg';'abcdef';'bcfgh';'bcdeg';....;'abefh';'abcegh'} numel(output) = 100
to this, started generating random numbers length of each partial string. generated random numbers corresponding each letter in each string. transferred numbers corresponding letters. comments should explain rest.
n=100 %// how many samples take c='abcdefgh' %// take samples these letters maxl=numel(c) %// longest string minl=5 %// shortest string len=randi([minl maxl],[n 1]) %// generate length of each partial string arrayfun(@(l) c(randsample(1:8,l)),len,'uni',0) %// randomly sample letters give strings of correct length
and n=4
gives, example
ans = 'cfhabedg' 'cfhabe' 'fahbe' 'dghfabe'
i'm not sure random because assumes there same number of strings of each length, don't think true. think len
should weighted respect number of strings of each length. think (but i'm not sure) should fix that:
for i=1:(maxl-minl+1) w(i)=factorial(minl-1+i)*nchoosek(maxl,minl-1+i); end len=minl-1+randsample(1:(maxl-minl+1),n,true,w./sum(w))
Comments
Post a Comment