Learning & Reasoning/R

alternative implement "The art of R programming" - 3.4.2 Extended Example

이현봉 2013. 2. 13. 01:03

For an old timer who had been doing some 2-D image processing stuff, the implementation below that simply gets the minimum of the upper half of the diagonal would be more recognizable

mind <- function(mt)
{
  size = nrow(mt)
  min = max(mt[1,])              # simple initialization.  can be set to any value other than the diagonal elements
  cities = c(NULL, NULL)
  for(r in 1:(size-1))
  {
    for(c in (r+1):size)
      if(mt[r,c]<min)
        { min=mt[r,c]
          cities = c(r,c)
        }
  }
  return(c(min, cities))
}