Gurkenkönig
Gurkenkönig

Reputation: 828

Strange matrix behaviour in r

If I do:

dim(my_matrix)
[1] 758289    768

typeof(my_matrix)
[1] "double"

max(my_matrix)
[1] 1

my_matrix[my_matrix<=0] = 0.0000001
my_matrix[my_matrix>=1] = 0.9999999

max(my_matrix)
[1] 1

I have no explanation for this behavior with smaller handmade matrixes it works without problems.

Upvotes: 0

Views: 67

Answers (1)

G. Grothendieck
G. Grothendieck

Reputation: 269481

If an element is less than 1 by a small amount you could get the behavior you noticed. The larger the matrix the larger the chance that an element is just a bit less than 1 which would explain why it did not happen for smaller matrices.

m <- matrix(0, 4, 4)
m[1,1] <- 1-1e-10
m.original <- m

max(m)
## [1] 1

m[m <= 0] <- 0.0000001
m[m >= 1] <- 0.9999999

max(m)
## [1] 1

Note that

print(max(m.original), digits = 12)
## [1] 0.9999999999

print(max(m), digits = 12)
## [1] 0.9999999999

Upvotes: 1

Related Questions