【发布时间】:2014-06-17 21:42:37
【问题描述】:
感谢this answer 的一些大力帮助,我写了一些应该的代码
- 在矩阵中随机选择一个单元格
matrnum - 将单元格从零更改为一,反之亦然
- 在对角三角形的相应单元格中进行相反的更改
- 检查条件2,看变化的新矩阵是否低于原矩阵
- 如果条件较低则返回新矩阵,如果不返回原始矩阵
- 每次都使用新的
rnum重复此操作,直到满足整体条件1
mat 在哪里:
mat <- matrix(rep(0, 100), nrow = 10)
mat[lower.tri(mat, diag = TRUE)] <- 1
while( condition1 > threshold) {
rnum <- sample(1:100, 1)
if(mat[rnum] > 0){
mat2 <- mat
mat2[rnum] <- 0
mat2[length(mat2) - rnum + 1] <- 1
return(mat2)
}else{
mat2 <- mat
mat2[rnum] <- 1
mat2[length(mat2) - rnum + 1] <- 0
return(mat2)
}
if(condition2(mat2) <
condition2(mat) ) {
mat <- mat2
}else{
mat
}
}
这里的条件不是关键,为了论证condition1和condition2,它可能是顶行的总和,即sum(mat[,1]),但我收到以下错误:
错误:没有函数可以返回,跳转到顶层
谁能指出我的缺陷?
【问题讨论】:
标签: r matrix while-loop