[R] create matrices with constraint

John McKown john.archie.mckown at gmail.com
Mon Dec 15 17:10:48 CET 2014


On Mon, Dec 15, 2014 at 10:06 AM, David L Carlson <dcarlson at tamu.edu> wrote:

> Actually there are not so many matrices as you suggest.
>
> > comb <- combn(28, 4)
> > dim(comb)
> [1]     4 20475
> > sum(comb[1,]==1)
> [1] 2925
> > comb[, 1]
> [1] 1 2 3 4
>
> There are 20,475 combinations, but you cannot choose any four to make a
> 4x7 matrix since each value can be used only once. The combn() function
> returns the combinations sorted, so we can get the number of combinations
> that contain 1 with sum(comb[1,]==1) and that is 2,925. The set of 4x7
> matrices cannot use the same combination more than once, so 2,925 is the
> maximum possible number of matrices and there may be fewer. As a first
> approach to finding them, you could take the first combination comb[, 1]
> which is 1, 2, 3, 4. Now add a second combination that does not include 1:4
> and then a third combination that does not include any in the first two
> combinations and finally a fourth that does not include any in the first
> three combinations. Actually this is easy since we will just take 1:4, 5:8,
> 9:12, 13:16, 17:20, 21:24, 24:18.
>
> > cols <- sapply(c(1, 5, 9, 13, 17, 21, 24), function(x)
> +  head(which(comb[1,]==x), 1))
> > cols
> [1]     1  9850 15631 18656 19981 20406 20471
> > comb[,cols]
>      [,1] [,2] [,3] [,4] [,5] [,6] [,7]
> [1,]    1    5    9   13   17   21   24
> [2,]    2    6   10   14   18   22   25
> [3,]    3    7   11   15   19   23   26
> [4,]    4    8   12   16   20   24   27
>
> But now it gets more complicated. While building the second matrix, we
> have to make sure that it does not use any combinations that have already
> been used.  Combinations used on earlier matrices may be necessary to
> complete later matrices and that is why the number of sets may be less than
> 2,925. This sequential approach would guarantee to obtain matrices meeting
> the OP's criteria, but would not necessarily produce the maximum number of
> matrices possible.
>

​Thanks. I was thinking that I was not on the right road, but just didn't
see what I was doing wrong. I appreciate the tutoring. And I'm bowing out
now because this is getting far beyond my expertise​ both in math and R.


>
> -------------------------------------
> David L Carlson
> Department of Anthropology
> Texas A&M University
> College Station, TX 77840-4352
>
>
-- 
​
While a transcendent vocabulary is laudable, one must be eternally careful
so that the calculated objective of communication does not become ensconced
in obscurity.  In other words, eschew obfuscation.

Maranatha! <><
John McKown

	[[alternative HTML version deleted]]



More information about the R-help mailing list