#### # function prototype #### # max.row.col(mat) # min.row.col(mat) # which.row.col(mat, value) # ############################################################## # obtain the row and column numbers of the maximum element of the matrix # "mat" max.row.col<-function(mat) { max.value<-max(mat) # the maximum value in the matrix "mat" # find the row and column numbers of the maximum value in "mat" res<-which.row.col(mat, value=max.value) # res contains 2 elements. The frist element is the row number of the # maximum value in "mat". The second element is the column number of the # maximum value in "mat". return(as.vector(res)) } # obtain the row and column number of the minimum element of the matrix mat min.row.col<-function(mat) { min.value<-min(mat) # the minimum value in the matrix "mat" # find the row and column numbers of the minimum value in "mat" res<-which.row.col(mat, value=min.value) # res contains 2 elements. The frist element is the row number of the # minimum value in "mat". The second element is the column number of the # minimum value in "mat". return(as.vector(res)) } # find the row and column numbers of "value" in a matrix mat # if there is a tie, then only one position is returned # If "value" is not in "mat", then the vector (0,0) will be returned. And a # warning message will be printed out. which.row.col<-function(mat, value) { mat<-as.matrix(mat); p<-ncol(mat); row<-0; col<-0; for(i in 1:p) { coli<-mat[,i] # the i-th column of the matrix "mat" tmp<-abs(coli-value) # check if "value" is in the i-th column tmppos<-which(abs(mat[,i]-value)<1.0e-6) if(length(tmppos)>0) { row<-tmppos[1] # the row number of "value" in "mat" col<-i # the column number of "value" in "mat" break } } res<-c(row, col) if(sum(res)==0) { cat("******** Warning!!! ********\n"); cat("the value ", value, " is not in the matrix >>\n"); print(mat); } return(as.vector(res)) }