因此,这里的消息:
谢谢你这么多的任何帮助您可以提供。
我为它非常感谢。
因此,这里的一些什么我已经试过:
cells<-c(0,.4,0,0,.25,.6,.25,.5,4,12,4,10,20,0,0,0)
Mu<-matrix(cells, 4,4, byrow=TRUE)
Ma<-list(Mu)
for(i in Ma){
if(i[2,4] > i[2,1]){
j <-length(Ma) + 1
c[Ma, j<-Mu]
}else if(i[2,4] < i[2,1]){
Ma[[i]] <- NULL
}
}
}
这是行不通的。
既然你需要能够添加或删除从阵列4个* 4矩阵,它可能是更容易使用,而不是一个阵列的4点* 4矩阵列表。
# Initial data
d <- replicate(3, matrix(sample(1:16),4,4), simplify=FALSE)
# Remove an element
remove <- function(d, i) {
d[[i]] <- NULL
d
}
# Duplicate an element
duplicate <- function(d, i) {
d <- append(d, list(d[[i]]))
d
}
# Example
d <- remove(d, 1)
d <- duplicate(d, 2)
d <- remove(d, 1)
d
如果性能是一个问题(列表被一次又一次地复制),你可能更愿意使用的环境来代替。
# Sample data
d <- replicate(3, matrix(sample(1:16),4,4), simplify=FALSE)
names(d) <- as.character(seq_along(d))
e <- as.environment(d)
# Remove an element
remove <- function(e, i) {
rm(list=as.character(i), envir=e)
e
}
# Duplicate an element
duplicate <- function(e, i) {
stopifnot( length(ls(e)) > 0 )
j <- max( as.numeric(ls(e)) ) + 1
assign( as.character(j), get( as.character(i), envir=e ), envir=e )
e
}
# Example (the elements are named, and their names do not change)
remove(e, 1)
duplicate(e, 3)
remove(e, 2)
as.list(e)
有了您的条件重复和搬迁,这成为:
# Sample data
d <- replicate(3, matrix(sample(1:16),4,4), simplify=FALSE)
names(d) <- as.character(seq_along(d))
e <- as.environment(d)
# Main loop
for(i in ls(e)) { # i is the name of the matrix
m <- get(i, envir=e) # The matrix itself
if(m[2,4] > m[2,1]) {
cat("Duplicating", i, "\n")
duplicate(e, i)
} else {
cat("Removing", i, "\n")
remove(e, i)
}
}
as.list(e)