[R] create matrices with constraint

John McKown john.archie.mckown at gmail.com
Sat Dec 13 18:10:43 CET 2014


On Fri, Dec 12, 2014 at 1:45 PM, John McKown <john.archie.mckown at gmail.com>
wrote:

>>
​<snip>​



> Hum. Consider the set of all possible unique 4 value vectors in which the
> values in the vectors are taken, without replacement, from the numbers 1
> through 28. This is a mathematical concept called "combination". In this
> case, there are 35 such: 7!/(4!*3!).
>

​Ack. I'm an idiot. That is 28!/(4!*24!) or 20,475 possible combinations.
The R code to generate a single matrix which contains all of the possible
cominations (which is not what is really wanted) could be:

    result<-c(0,0,0,0); #initialize to something
    for(i1 in 1:25) {
        for (i2 in (i1+1):26) {
            for (i3 in (i2+1):27) {
                for (i4 in (i3+1):28) {
                    x<-c(i1,i2,i3,i4);
                    result<-rbind(result,x);
                }
            }
        }
    }
    result<-result[2:(nrow(result)-1),]; #strip off that first row​

​of c(0,0,0,0)​


​The problem now would be to assign those vectors to the proper matrix. My
first though is to have the result be a data.frame or a three dimensional
"matrix". But actually _doing_ that I haven't figured out yet. Oh, and
obviously from the way that I generated the vectors, the data items within
each vector is sorted in ascending value. An even more difficult problem
would be if the OP needed every possible permutation of each possible
matrix.​


-- 
The temperature of the aqueous content of an unremittingly ogled
culinary vessel will not achieve 100 degrees on the Celsius scale.

Maranatha! <><
John McKown

	[[alternative HTML version deleted]]



More information about the R-help mailing list