Skip to content
Prev 284734 / 398502 Next

how to exclude rows with not-connected coalitions

On Thu, Feb 09, 2012 at 12:18:06PM +0000, Schumacher, G. wrote:
Hi.

If i understand correctly, a connected coalition is
an interval on your scale. If this is correct, try
the following for generating only the intervals.

  n <- 7
  a <- matrix(nrow=n+choose(n, 2), ncol=n)
  k <- 1
  for (i in 1:n) {
      for (j in (i+1):(n+1)) {
          x <- rep(0, times=n+1)
          x[i] <- 1
          x[j] <- 1
          a[k,] <- cumsum(x)[1:n]
          k <- k+1
      }
  }
  a[a == 2] <- 0
  a

        [,1] [,2] [,3] [,4] [,5] [,6] [,7]
   [1,]    1    0    0    0    0    0    0
   [2,]    1    1    0    0    0    0    0
   [3,]    1    1    1    0    0    0    0
   [4,]    1    1    1    1    0    0    0
   [5,]    1    1    1    1    1    0    0
   [6,]    1    1    1    1    1    1    0
   [7,]    1    1    1    1    1    1    1
   [8,]    0    1    0    0    0    0    0
   [9,]    0    1    1    0    0    0    0
  [10,]    0    1    1    1    0    0    0
  ...

If you already have the matrix of all coalitions
and want to detect the connected ones, try the
function ?rle. There should be exactly one
run of ones.

Hope this helps.

Petr Savicky.