我有一个矩阵(我们称它为output):
> output
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 1 1 0 0 1
[2,] 1 1 1 0 0 1
[3,] 1 1 1 0 0 1
[4,] 0 0 0 0 0 0
[5,] 1 0 1 1 0 0
[6,] 1 0 1 1 0 0
我希望做的是通过每组两行相加,创建一个新的矩阵 (output2)。例如,新矩阵的第一行将会是:
output2[1,] <- output[1,] + output[2,]
新矩阵output2的第二行将由旧输出矩阵的第三行和第四行之和组成:
output2[2,] <- output[3,] + output[4,]
等等。考虑到我需要对更大的矩阵执行相同的操作,你认为最好的编码方式是什么?我正在考虑使用seq()函数和for循环创建虚拟变量。
有更简单的方法吗?
编辑:
dput(output)
结构(c(1,1,1,0,1,1,1,1,1,1,1,1,1,0,0,0,1, 1,0,1,1,1,1,0,1,1,1,1,1,1,0,0,0,0,1,1,0,0, 1,0,0,0,0,0,0,0,0,1,0,0,1,1,1,0,0,0,1,0,0, 0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1,1,0,0,1, 0,0,0,0,0,1,1,0,0,1,0,0,0,0,0,1,1,0,0,1, 0,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0, 0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0, 0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1,0,0, 0,0,0,0,0,0,1,0,0,0,0,0,0,0),.Dim = c(10L,19L))
dput(yourMatrix)
。 - Andre Elrico