Generate all possible subset from a character array in MATLAB

I need to generate all possible subset from a character array with reduced execution time. Actual input is of length '500' characters and maximum length of subset is limited to 20 characters.
For example:
input='ABCA';
output ='A', 'B', 'C', 'AB', 'BC', 'CA', 'ABC', 'BCA', 'ABCA'

4 Kommentare

madhan ravi
madhan ravi am 30 Mär. 2019
Bearbeitet: madhan ravi am 30 Mär. 2019
But you have only 19 in the output?
example output have 9 subsets with length of characters in the subsets range from 1 to 4. The maximum length of characters in a subset is limited to 20 characters.
Could you confirm that length(unique(input)) is 500? For example 500 Chinese ideographs? As opposed to length(input) being 500 but the number of unique being much smaller?
length(input) is 500

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Sangeetha R
Sangeetha R am 30 Mär. 2019
Bearbeitet: Sangeetha R am 30 Mär. 2019

0 Stimmen

Answer that i found is given below,
tic
input='MERASLIQKAKLAEQAERYEDMAAFMKGAVEKGEELSCEERNLLSVAYKNVVGGQRAAWRVLSSIEQKSNEEGSEEKGPEVREYRVFYLKMKGDYYRYLAEVATGDDKKRIIDSARSAYQEAMDISKKEMPPTNPIRLGLALNFSVFHYEIANSPEEAISLAKTTFDEAMADLHTLSEDSYKDSTLIMQLLRDNLTLWTADNAGEEGGEAPQEPQSMERASLIQKAKLAEQAERYEDMAAFMKGAVEKGEELSCEERNLLSVAYKNVVGGQRAAWRVLSSIEQKSNEEGSEEKGPEVREYRVFYLKMKGDYYRYLAEVATGDDKKRIIDSARSAYQEAMDISKKEMPPTNPIRLGLALNFSVFHYEIANSPEEAISLAKTTFDEAMADLHTLSEDSYKDSTLIMQLLRDNLTLWTADNAGEEGGEAPQEPQSSEDSYKDSTLIMQLLRDNLTLWTADNAGEEGGEAPQEPQSMERASLIQKAKLAEQAERYEDMAAFMKGAVEKGEELSCEERNLLSVAYKNVVGGQRAAW';
l=length(input);
begn=1;
count=1;
output_seq=cell(1000,1);%preallocating
for i=1:l
if(begn<=l)
[count,output_seq]=pep(begn,l,input,count,output_seq);
begn=begn+1;
end
end
output_unique=unique(output_seq);
toc
function [count,output_seq]=pep(b,l,input,count,output_seq)
for i=0:l
if(b+i-b<21)
if(b+i<=l)
p = input(b:b+i);
output_seq{count}=p;
count=count+1;
end
end
end
end
Consider
[S(1:end-2);
S(2:end-1);
S(3:end)].'
Now unique rows

Kategorien

Produkte

Version

R2018b

Tags

Gefragt:

am 30 Mär. 2019

Beantwortet:

am 30 Mär. 2019

Community Treasure Hunt

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

Start Hunting!

Translated by