次のようなカーネルマトリックスがあります。
kern <- matrix(c(1,0,0,1,0,0,0,1,1,0,1,1,0,0,1,0,0,1), dimnames=list(c("r1", "r1", "r3"), c("c1a", "c1b", "c2a", "c2b", "c3a", "c3b")), ncol=6, nrow=3)
> kern
c1a c1b c2a c2b c3a c3b
r1 1 1 0 0 0 0
r2 0 0 1 1 0 0
r3 0 0 1 1 1 1
kern[,c("c1b", "c2b", "c3b")]
ここで、単位行列のような行操作を適用したいと思います。これは、3 番目の行から 2 番目の行を引くことで簡単に達成できることを知っています。
kern[3,] = kern[3,] - kern[2,]
、
しかし、私のためにそれを行うRの関数はありますか? 別のスレッドに投稿された縮小行階層フォームの関数は、私が必要としているものではありません。
編集
私は不器用な解決策を持っています
sub <- kern[,c("c1b", "c2b", "c3b")]
for (i in which(colnames(kern) %in% colnames(sub))){
##identify which columns have more than one entry
nonzero.row.idx <- which(kern[,i] != 0)
while(length(nonzero.row.idx) > 1){
row.combinations <- combn(nonzero.row.idx, 2)
for (j in ncol(row.combinations)){
r1.idx <- row.combinations[1,j]
r2.idx <- row.combinations[2,j]
r1 <- kern[r1.idx,]
r2 <- kern[r2.idx,]
if (min(r1 - r2) >=0)
kern[r1.idx, ] <- r1-r2
else if (min(r2 - r1) >=0)
kern[r2.idx, ] <- r2-r1
else
stop("Producing negative entries in row")
nonzero.row.idx <- which(kern[,i] != 0)
}
}
}
kern[,c("c1b", "c2b", "c3b")]
kern
また、入力を負にしたくないことを忘れていました。このコードは私のいくつかの例では機能しますが、他の多くの行列では問題を引き起こす傾向があります。