Hi i have a dilemma here. I have here with me a following matrix:
[,1] [,2] [,3] [,4] [,5]
[1,] 0.000000 1.414214 2.828427 4.242641 5.656854
[2,] 1.414214 0.000000 1.414214 2.828427 4.242641
[3,] 2.828427 1.414214 0.000000 1.414214 2.828427
[4,] 4.242641 2.828427 1.414214 0.000000 1.414214
[5,] 5.656854 4.242641 2.828427 1.414214 0.000000
My question here is how do I single out the least non-zero value in the matrix above. Clearly, if I used min(A)
I would get 0 as my answer but what I want is the value 1.414214
.
min of a where a is not equal to 0
min(a[a!=0])
With a function to print index:
min_value <- function(M){
minval <- min(M[M!=0])
index <- which(M==minval, arr.ind=TRUE)
print(paste("The smallest non-zero value (", minval, ") is located in:", sep=""))
for(i in 1:nrow(index)){
print(paste("row[", index[i, 1] ,"] and column[", index[i, 2], "]", sep="" ))
}
return(list(min_value=minval, index=index))
}