How to find max value in matrix excluding values < 1? I have extracted the max value using the code below, but now unsure how to add a condition that values of 1 be excluded in this computation.

```
max(as.numeric(unlist(df)))
```

How to find max value in matrix excluding values < 1? I have extracted the max value using the code below, but now unsure how to add a condition that values of 1 be excluded in this computation.

```
max(as.numeric(unlist(df)))
```

Something doesn't sound right. Suppose all the values are greater than or equal to 1. Then the max of the matrix gives you the answer and it meets your condition. Suppose some values are less than one and others aren't. Then the max is greater than 1 and you can ignore the condition. Suppose all the values are less than 1. Then there is no solution.

Thank you @startz you bring up some good points. Essentially, I am wanting to figure out a way to extract the maximum value on the off-diagonals of the matrix (which are all 1). I thought of removing all the 1s in the matrix and then computing the max value but wasn't sure how to in matrix form.

I could do this exercise visually, but thought it best to code it up. The values on the off-diagonals will all be less than 1 as it is a correlation matrix. I wish to find the max out of that sample of values.

How would you suggest I proceed?

I bet there is a more elegant way to do this but you can set the diagonal elements to -Inf.

```
#Make a matrix with 1 on the diagonal and <1 off diagnal
set.seed(123)
MAT <- matrix(runif(16),nrow = 4)
diag(MAT) <- 1
MAT
#> [,1] [,2] [,3] [,4]
#> [1,] 1.0000000 0.9404673 0.5514350 0.6775706
#> [2,] 0.7883051 1.0000000 0.4566147 0.5726334
#> [3,] 0.4089769 0.5281055 1.0000000 0.1029247
#> [4,] 0.8830174 0.8924190 0.4533342 1.0000000
#set the diagonal to -Inf, then find the remainiong maximum
diag(MAT) <- -Inf
MAT
#> [,1] [,2] [,3] [,4]
#> [1,] -Inf 0.9404673 0.5514350 0.6775706
#> [2,] 0.7883051 -Inf 0.4566147 0.5726334
#> [3,] 0.4089769 0.5281055 -Inf 0.1029247
#> [4,] 0.8830174 0.8924190 0.4533342 -Inf
max(MAT)
#> [1] 0.9404673
```

^{Created on 2022-11-14 with reprex v2.0.2}

Or just

```
df <- as.numeric(unlist(df))
max(df[df<1])
```

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.