如何在第一列中只有最小元素的矩阵中查找行?

How to find rows in a matrix with only the minimum element in the first column?

我正在考虑 Nelder Mead 优化技术,我需要 return 第一列中具有最小值的矩阵。

我的代码:


fun <- function(x){
  3*(sin(0.5+0.25*x[2]*x[1]))*cos(x[1])
}
out <- matrix(NA, nrow=100, ncol=3)
for (i in 1:100) {
  x <- runif(1, -7, 7)
  y <- runif(1, -7, 7)
  x0 <- c(x,y)
  res <- optim(x0,fun,method="Nelder-Mead")  
  out[i,] <- round(c(res$value,res$par) , digits = 5)
} 
out

使用第一列的 min 创建一个逻辑向量,并对矩阵进行子集化

out1 <- out[out[,1] == min(out[,1]),]