Hello all. This is the first time I am posting on this forum - but that's only because I've always found a pertinent thread that has answered all my questions. So firstly, thank you! The forum has grown quite exhaustive since the first time I used it in 2008.
I was wondering whether there was any efficient method of calculating a string of all possible combinations of length k from a string of variables of length n. My solution would be a set of loops with nested if functions and a string that archives the combinations created and checks every new combination against that combination to see whether it already exists. However I feel that this repeated looking up of the string will not be efficient and would possibly slow the program down, so wondering whether anyone can think of an algorithm that cuts down on the number of steps while creating the string of all possible combinations. As an illustration n=50 and k=5.
Thanks a ton!
Combinations
Moderators: EViews Gareth, EViews Moderator, EViews Jason, EViews Matt
-
EViews Glenn
- EViews Developer
- Posts: 2682
- Joined: Wed Oct 15, 2008 9:17 am
Re: Combinations
I'm a little confused by the question. Are there n-variables from which you choose k variables, or a string of length n from which you choose k characters? And if the latter, is the selected string a consecutive string of k characters from the string of length n or something else, like k arbitrary characters from the string?
-
startz
- Non-normality and collinearity are NOT problems!
- Posts: 3797
- Joined: Wed Sep 17, 2008 2:25 pm
Re: Combinations
To follow-up on Glenn's comment, note that you are talking about over two million combinations. You're going to have to think carefully about how you're going to store the results.
Re: Combinations
Hey, guys! I was looking for this code on eViews and I found this post which was unanswered. As I made my own code, I hope it might be useful for someone. Enjoy it!
Code: Select all
logmode logmsg
call Combinations("", "a b c d e", 1, 3)
subroutine Combinations(string %prefix, string %list_elements, scalar !index, scalar !p)
'~~> Quando o prefixo atinge p elementos
if !p=0 then
logmsg %prefix
return
endif
'~~> Quando o index atinge o total de elementos da lista
if !index=@wcount(%list_elements)+1 then
return
endif
'~~> Elemento a ser analisado
%element=@word(%list_elements,!index)
'~~> Gerar todas as combinações nas quais %element está presente em %prefix (Chamada Recursiva)
if @isempty(%prefix)=0 then
%prefix=%prefix + " " + %element '~~> Inclui elemento no prefixo (subset)
else
%prefix=%element
endif
call Combinations(%prefix, %list_elements, !index+1, !p-1) '~~> Analisa o próximo elem (!index=!index+1)
'~~> Gerar todas as combinações nas quais %element NÃO está presente em %prefix (Chamada Recursiva)
%prefix=@wleft(%prefix,@wcount(%prefix)-1) '~~> Remove elemento no prefixo (subset)
call Combinations(%prefix, %list_elements, !index+1, !p) '~~> Analisa o próximo elem (!index=!index+1)
endsub
Who is online
Users browsing this forum: No registered users and 2 guests
