仅使用几列来更改二进制矩阵中的值

时间:2017-11-22 14:15:18

标签: r

考虑8乘6的二进制矩阵M

M <- matrix(c(0,0,1,1,0,0,1,1,
          0,1,1,0,0,1,1,0,
          0,0,0,0,1,1,1,1,
          0,1,0,1,1,0,1,0,
          0,0,1,1,1,1,0,0,
          0,1,1,0,1,0,0,1),nrow = 8,ncol = 6)

以下是M

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

以下矩阵包含矩阵1M的列索引

    [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    3    2    5    2    3    2
[2,]    4    3    6    4    4    3
[3,]    7    6    7    5    5    5
[4,]    8    7    8    7    6    8

让我们来表示

ind <- matrix(c(3,4,7,8,
                2,3,6,7,
                5,6,7,8,
                2,4,5,7,
                3,4,5,6,
                2,3,5,8),nrow = 4, ncol=6)

我正在尝试仅在1的某些列中将0的单个位置更改为M。 例如,考虑每两列更改两个的情况。一种可能性是在前两列中改变两个位置。设N是得到的矩阵。这将生成以下矩阵N

N <- matrix(c(0,0,0,1,0,0,1,1,
              0,1,1,0,0,0,1,0,
              0,0,0,0,1,1,1,1,
              0,1,0,1,1,0,1,0,
              0,0,1,1,1,1,0,0,
              0,1,1,0,1,0,0,1),nrow = 8,ncol = 6)

这是N

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

对于N生成的矩阵的 EACH ,我会进行以下计算。

X <- cbind(c(rep(1,nrow(N))),N)
ans <- sum(diag(solve(t(X)%*%X)[-1,-1]))

然后,我想获得矩阵N,它产生ans的最小值。这个8乘6矩阵只是一个例子。我该怎么做?

我问了一个类似于这个问题的问题,然后才改变每一栏的立场。这是link to that

0 个答案:

没有答案