Home > Software engineering >  How to find rows in a matrix with only the minimum element in the first column?
How to find rows in a matrix with only the minimum element in the first column?

Time:11-28

I am considering Nelder Mead optimization technique and I need to return the matrix with the minimum value in the first column.

My code:


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

CodePudding user response:

Create a logical vector with the min of the first column and subset the matrix

out1 <- out[out[,1] == min(out[,1]),]
  •  Tags:  
  • r min
  • Related