Erroneous input into the allcomb function

1 view (last 30 days)
Dear everybody,
I have been trying to generate a code that could do an iterative truncation of a letter sequence combined with the replacement of some letters with subarrays of additional letters. The subarrays and the leftover letters are intended to be used in the allcomb function that would generate all combinations. (link: https://uk.mathworks.com/matlabcentral/fileexchange/10064-allcomb-varargin-). I have shortened the letter sequence (H3_seq) and used the letters in alphabetical order to facilitate troubleshooting. This is my best bet so far with a for loop and a replacement function:
H3_seq = 'ABCDE'
H3_length = length(H3_seq);
for n = 1:H3_length
N_seq = H3_seq(1:n);
N_length = length(N_seq);
S1 = strrep(N_seq,'A','{''F'',''G'',''H'',''I'',''J''}');
S2 = strrep(S1,'B','{''K'',''L'',''M'',''N'',''O'',''P''}');
S3 = strrep(S2,'C','{''Q'',''R'',''S''}');
S4 = strrep(S3,'D','{''T''}');
S5 = strrep(S4,'E','{''O''}')
S6 = strrep(S5,'}{','},{');
Note that
allcomb(S6)
does not work. It just transposes the letter sequence of S6 without the receiving the combinations.
However, by copying the value of S6 directly I do receive the correct output.
allcomb({'F','G','H','I','J'},{'K','L','M','N','O','P'},{'Q','R','S'},{'T'},{'O'})
ans =
90×5 cell array
'F' 'K' 'Q' 'T' 'O'
'F' 'K' 'R' 'T' 'O'
'F' 'K' 'S' 'T' 'O'
'F' 'L' 'Q' 'T' 'O'
'F' 'L' 'R' 'T' 'O'
'F' 'L' 'S' 'T' 'O'
'F' 'M' 'Q' 'T' 'O'
'F' 'M' 'R' 'T' 'O'
'F' 'M' 'S' 'T' 'O'
'F' 'N' 'Q' 'T' 'O'
'F' 'N' 'R' 'T' 'O'
'F' 'N' 'S' 'T' 'O'
'F' 'O' 'Q' 'T' 'O'
'F' 'O' 'R' 'T' 'O'
'F' 'O' 'S' 'T' 'O'
'F' 'P' 'Q' 'T' 'O'
'F' 'P' 'R' 'T' 'O'
'F' 'P' 'S' 'T' 'O'
'G' 'K' 'Q' 'T' 'O'
'G' 'K' 'R' 'T' 'O'
'G' 'K' 'S' 'T' 'O'
'G' 'L'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' 'O'
'J' 'K' 'Q' 'T' 'O'
'J' 'K' 'R' 'T' 'O'
'J' 'K' 'S' 'T' 'O'
'J' 'L' 'Q' 'T' 'O'
'J' 'L' 'R' 'T' 'O'
'J' 'L' 'S' 'T' 'O'
'J' 'M' 'Q' 'T' 'O'
'J' 'M' 'R' 'T' 'O'
'J' 'M' 'S' 'T' 'O'
'J' 'N' 'Q' 'T' 'O'
'J' 'N' 'R' 'T' 'O'
'J' 'N' 'S' 'T' 'O'
'J' 'O' 'Q' 'T' 'O'
'J' 'O' 'R' 'T' 'O'
'J' 'O' 'S' 'T' 'O'
'J' 'P' 'Q' 'T' 'O'
'J' 'P' 'R' 'T' 'O'
'J' 'P' 'S' 'T' 'O'
There is clearly something wrong with my formatting of the input, but I have tried various combinations of arrays, cells, and tables without much luck. I have already made a code for just calculating the number of combinations, but I would like to receive each letter sequence so that I could couple them with a number. The number is linked to sequence and I would like to search this number and receive the specific combination that matches that number. The number would be the sum of some values that each letter represent, so my idea is to run the allcomb and replacement function again but with my numerical values. However, I can properly solve this myself if I get some help with formatting the input to the allcomb function.
This is my continuation of the first code that does not function properly.
capExpr = '{';
spaceExpr = '}';
capStartIndex = regexp(S6,capExpr);
spaceStartIndex = regexp(S6,spaceExpr);
combi = combine(capStartIndex,spaceStartIndex)
Cell={};
for z = 1:length(N_seq);
S7 = S6(combi(1,z):combi(2,z))
for x =1:max(size(S7))
Cell{x}=S7(x);
end
end
allcomb(Cell{:})
end
Sincerely,
Christoffer H.
The University of Copenhagen // The University of Oxford

Answers (2)

Nirav Sharda
Nirav Sharda on 23 Feb 2017
The issue is occurring due to the fact that the function takes in multiple cell-arrays and you are passing it a character vector. You can try to create cell arrays based on your need and pass them as input to the function. I hope this helps!

Guillaume
Guillaume on 23 Feb 2017
The single string '{''F'',''G'',''H'',''I'',''J''},{''K'',''L'',''M'',''N'',''O'',''P''},{''Q'',''R'',''S''},{''T''},{''O''}' has nothing in common with the multiple cell arrays of various size {'F','G','H','I','J'}, {'K','L','M','N','O','P'}, {'Q','R','S'}, {'T'}, and {'O'}.
The ultimate aim of your algorithm is unclear. To obtain the correct result with your example you could do:
H3_seq = 'ABCDE';
split_seq = num2cell(H3_seq);
replacements = {'A', {'F','G','H','I','J'}
'B', {'K','L','M','N','O','P'}
'C', {'Q','R','S'}
'D' ,{'T'}
'E', {'O'}};
%replacement is a two columns cell array.
%1st column is original character, 2nd column is a CELL ARRAY of character
[~, row] = ismember(split_seq, replacements(:, 1));
split_seq = replacements(row, 2);
allcombs(split_seq{:})

Categories

Find more on Characters and Strings in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!