comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » Re: Selecting groups of 5 coords from a set of n (nC5)
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
Re: Selecting groups of 5 coords from a set of n (nC5) [message #47833] Fri, 03 March 2006 10:00
Olivia is currently offline  Olivia
Messages: 16
Registered: February 2006
Junior Member
Wow, thanks Paolo! That is far more elegant than my attempts. You are
fast becoming the most important person in my masters project! Thanks
again for all your help,

Olivia
Re: Selecting groups of 5 coords from a set of n (nC5) [message #47834 is a reply to message #47833] Fri, 03 March 2006 09:19 Go to previous message
Paolo Grigis is currently offline  Paolo Grigis
Messages: 171
Registered: December 2003
Senior Member
Some time ago I needed combinations and I came up with
this function. Example:

IDL> print,pgcomb(5,3)
0 1 2
0 1 3
0 1 4
0 2 3
0 2 4
0 3 4
1 2 3
1 2 4
1 3 4
2 3 4


It might not be the most efficient solution, but at least
I understand the (quite simple) underlying algorithm.

Be careful that no check is made for bad inputs, it's
up to the user to make sure that 1<=j<=n.

Ciao,
Paolo


FUNCTION pgcomb,n,j
;;number of combinations of j elements chosen from n
nelres=long(factorial(n)/(factorial(j)*factorial(n-j)))

res=intarr(j,nelres);array for the result
res[*,0]=indgen(j);initialize first combination

FOR i=1,nelres-1 DO BEGIN;go over all combinations
res[*,i]=res[*,i-1];initialize with previous value

FOR k=1,j DO BEGIN;scan numbers from right to left

IF res[j-k,i] LT n-k THEN BEGIN;check if number can be increased

res[j-k,i]=res[j-k,i-1]+1;do so

;if number has been increased, set all numbers to its right
;as low as possible
IF k GT 1 THEN res[j-k+1:j-1,i]=indgen(k-1)+res[j-k,i]+1

BREAK;we can skip to the next combination

ENDIF

ENDFOR

ENDFOR

RETURN,res

END



Olivia wrote:
> Dear All,
>
> I am trying to write a loop to perform a calculation on all possible
> sets of 5 coordinates from a group of n. The test case I am working on
> has a total number of coordinates of 8, so there will be 8c5=56 unique
> solutions. At the moment, I am thinking of using 5 for loops as an
> extension of a similar problem I worked on choosing 3 points. The 3
> point code ran like this:
>
> ;Select groups of 3 boundary points
> ;for p=1, n, 1 do begin
> ; for k=1, n, 1 do begin
> ; for m=0, n-1, 1 do begin
> ; ellipse_points=[[bx[m], by[m]],$
> ; [bx[m+k], by[m+k]],$
> ; [bx[m+k+p], by[m+k+p]]]
>
> On reflection, I was wondering if there might be a better way of doing
> this. This is probably only a fear as my code is already looking really
> complicated and I am worried about putting rubbish in and getting
> rubbish out whilst being completely unaware. If anyone has any ideas I
> would be really grateful to hear them. Thank you very much,
>
> Olivia
>
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Selecting groups of 5 coords from a set of n (nC5)
Next Topic: Displaying equations in ps output

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Wed Oct 08 15:34:55 PDT 2025

Total time taken to generate the page: 0.00514 seconds