2

I have some string like this:

'RRSSTT'
'RRRSSSTTT'
'RRRRSSSSTTTT'
 etc...

I need to extract all unique permutation with this property:

  • 'RRSSTT' ; 'RRTTSS' ; 'SSRRTT' ; 'SSTTRR' ; 'TTRRSS' ; 'TTSSRR' are all the same string, and
  • 'RSTRST' ; 'RTSRTS' ; 'TSRTSR' ; 'TRSTRS' ; 'SRTSRT' ; 'STRSTR' are all the same string.

With command perms I obtain all permutations (if string have 6 elements, permutations are 720). With uperms script I obtain all unique permutation (if string have 6 elements, permutations are 90). I've found uperm on mathworks (http://www.mathworks.com/matlabcentral/fileexchange/27321-unique-random-permutations).

Until here all works, but I need to write/find an other algorithm that generate permutations with above special property. If string is 'RRSSTT' the desired permutations have to be 15.

Someone already faced to this problem and could help me?

------EDIT------

There are 15 goups of equivalent string, some example are:

Group 1:          Group 6:        Group 11:

'RSTTSR'         'RSTSTR'         'RSSTRT'
'SRTTRS'         'SRTRTS'         'STSRRT'
'STRRTS'         'STRTRS'         'RSRTTS'
'RTSSTR'         'TRSRST'         'STTRSR'
'TRSSRT'         'TSRSRT'         'TRRSTS'
'TSRRST'         'RTSTSR'         'TRTSSR'

Group 2:          Group 7:        Group 12:

'RSTRST'         'RSSRTT'         'RSSTTR'
'SRTSRT'         'SSTRRT'         'SRRTTS'
'STRSTR'         'RRSTTS'         'STTRRS'
'RTSRTS'         'STTSRR'         'TRRSST'
'TRSTRS'         'TRRTSS'         'TSSRRT'
'TSRTSR'         'TTRSSR'         'RTTSSR'

Group 3:          Group 8:        Group 13:

'RSTSRT'         'SRRSTT'         'SRSTTR'
'STRSRT'         'SSRTTR'         'SRRTST'
'RSTRTS'         'RRTSST'         'RTRSST'
'STRTSR'         'TSSTRR'         'TSSRTR'
'TRSRTS'         'RTTRSS'         'TSTRRS'
'TRSTSR'         'TTSRRS'         'RTTSRS'

Group 4:          Group 9:        Group 14:

'SRTRST'         'RSRSTT'         'RRSSTT'
'SRTSTR'         'SSRTRT'         'SSRRTT'
'RTSRST'         'STSTRR'         'SSTTRR'
'TSRSTR'         'RRTSTS'         'RRTTSS'
'RTSTRS'         'TRTRSS'         'TTRRSS'
'TSRTRS'         'TTSRSR'         'TTSSRR'

Group 5:          Group 10:        Group 15:

'STRRST'         'SRSRTT'         'SRSTRT'
'RSTTRS'         'SSTRTR'         'RSRTST'
'SRTTSR'         'RRSTST'         'STSRTR'
'RTSSRT'         'TSTSRR'         'RTRSTS'
'TRSSTR'         'RTRTSS'         'TSTRSR'
'TSRRTS'         'TTRSRS'         'TRTSRS'

I need to return one string for each group, I prefer the string with R as firs letter. Sorry for notation change.

-----EDIT 1-----

I will try to explain why

str1='RSTRST' 

and

str2='STRSTR' 

are equivalent.

You have to consider 'R' in str1 and substitute it with 'S', substitute 'S' with 'T' and finally 'T' with 'R'. All strings in a group are right, but in each group all strings are equivalent and I need to take only one string from each group.

As beaker said, the rule of character replacement doesn't work always, I need to find the right role.

Pietro Z.
  • 521
  • 2
  • 6
  • 18
  • 2
    I don't really see which permutations you want to keep. Can you list the allowed permutations of `'aabbcc'`? Or tell us the rule you use to get the permutations? – beaker Jan 29 '16 at 16:31
  • @beaker I've added some example, sorry for change of notation. – Pietro Z. Jan 29 '16 at 16:58
  • I don't understand the meaning of the four groups. For one input example, can you give all 15 permutations you expect? And some examples which should not be included with the reason (e.g. not include ABC because CDE is already included) – Daniel Jan 29 '16 at 17:02
  • So you're considering all cyclic permutations of the two halves of the string to be identical? – beaker Jan 29 '16 at 17:06
  • No, that's not it. I don't know how you're choosing these groups. As @Daniel says (and I said before), a list of all 15 allowed permutations might help. – beaker Jan 29 '16 at 17:09
  • In the second edit i've tried to explain better why elements are equivalent. Sorry, but it is very complicated. Thanks for patience – Pietro Z. Jan 29 '16 at 17:20
  • @daniel, have you understand what I trying to explain? – Pietro Z. Jan 29 '16 at 17:39
  • @PietroZ. I think understood why you said the strings were equivalent (although your edit is even more confusing than before). What I **don't** understand is what strings you think are **not** equivalent and how you arrive at a total of 15 allowed permutations. Because from what I see, there are only 4 that are not equivalent: `RSTRST`, `RSTRTS`, `RTSRST` and `RSTRST`. – beaker Jan 29 '16 at 18:42
  • @beaker I've added more groups. Try to use `uperm('RRSSTT')` and you will get 90 unique permutation. Using substitution role explained in second edit you can verify that there are 15 groups with 6 equivalent permutations. I would take one string for each group. The equivalent permutations in each group are always 6 because there are tree letters: 'R' 'S' 'T' and `perms('RST')` are six. – Pietro Z. Jan 30 '16 at 13:22
  • I don't understand the purpose of the empty groups – Daniel Jan 30 '16 at 14:47
  • 1
    @Daniel As soon as is possible I will complete all other groups – Pietro Z. Jan 30 '16 at 14:48
  • 2
    Your groups are inconsistent. Some permutations within a group are character replacements of other strings in the same group, others are cyclic rotations. You need to tell us what rule you use to choose groups (or correct your example). For instance, in Group 7: `'RSSRTT'`, `'RRSTTS'`, `'TRRTSS'`. How can these 3 strings all be related to each other? – beaker Jan 30 '16 at 16:16
  • @beaker you are right, the rule of character replacement doesn't work always. **I'm 100% sure that groups are right**. I need time to check the physical related problem and find a rule that we can use to choose groups. As you said it seems that there are two rules: character replacements and cyclic rotations – Pietro Z. Jan 30 '16 at 16:34

1 Answers1

1

I think now I understood your code, this would solve it:

  • Use uperm to generate a set with all possible solution.
  • Call [a]=unique(solution,'stable') for each solution returned by uperm and accept it only if RST is returned (or any fixed order).
Daniel
  • 36,610
  • 3
  • 36
  • 69
  • Thanks, I'm trying to use it and I will say to you something soon. – Pietro Z. Jan 30 '16 at 10:26
  • I've tried, but I think that isn't right way. Try to watch group 3, there are two strings where `unique(solution,'stable')` return `RST`. On the opposite, in group 8, there are no strings that return `RST`. Maybe there aren't visible correlations between strings and I can group these strings only simulating the physical problem. @daniel – Pietro Z. Feb 04 '16 at 08:17
  • I just realized I had a wrong understanding of your groups because. Why are RSTSRT and RSTRTS the same? I would have expected them in different groups. – Daniel Feb 04 '16 at 11:04