0

I am trying to classify matrices based on which row has the highest value. My code works correctly, except for when the first and third row have the same value - then the output is that the third value is larger than the first. For example, when I use the following matrix:

        , , 1

           [,1]   [,2]   [,3]
    [1,] 0.5+0i 0.5+0i 0.5+0i
    [2,] 0.0+0i 0.0+0i 0.0+0i
    [3,] 0.5+0i 0.5+0i 0.5+0i 

And the following bit of the code:

  null   <- 0
  neg   <- 0
  pos   <- 0
  equal <- 0

  for(i in 1){
   if(Re(pie.ch[1,1,i])>Re(pie.ch[2,1,i]) &&
      Re(pie.ch[1,1,i])>Re(pie.ch[3,1,i])){
         neg<-neg+1
    }
    if(Re(pie.ch[3,1,i])>Re(pie.ch[1,1,i]) &&
       Re(pie.ch[3,1,i])>Re(pie.ch[2,1,i])){
          pos<-pos+1
    }


  if(Re(pie.ch[2,1,i])>Re(pie.ch[1,1,i]) &&
  Re(pie.ch[2,1,i])>Re(pie.ch[3,1,i])){
      null<-null+1
  }
  if((Re(pie.ch[1,1,i])==Re(pie.ch[2,1,i]) && 
    Re(pie.ch[1,1,i])>Re(pie.ch[3,1,i]))||
    (Re(pie.ch[1,1,i])==Re(pie.ch[3,1,i])&&
    Re(pie.ch[1,1,i])>Re(pie.ch[2,1,i])||
  (Re(pie.ch[2,1,i])==Re(pie.ch[3,1,1])&&
  Re(pie.ch[3,1,i])>Re(pie.ch[1,1,i]))|| 
  Re(pie.ch[1,1,i])==Re(pie.ch[2,1,i]) &&
  Re(pie.ch[2,1,i])==Re(pie.ch[3,1,i]))){
     equal<-equal+1

 }
}
    null
    neg
    pos
    equal

I get the following output, which is clearly wrong:

> null
[1] 0
> neg
[1] 0
> pos
[1] 1
> equal
[1] 0

However, the code works correctly if all the three values are the same (I tried it for all values being 0), giving me a 1 for Equal and a 0 for Pos.

Any ideas why this error might occur? Thank you very much!

De Novo
  • 7,120
  • 1
  • 23
  • 39
  • If I understand correctly, you can match these values based on position as well? So if value from first column is max, you would deem it negative, positive for the value from third column and so on? Zdravo. :) – Roman Luštrik Jun 17 '18 at 06:36
  • I'm not sure if I understand exactly what you mean. Basically, these matrices represent stationary states of Markov chains, so the value from the first row is the probability of a population having a negative growth rate, the third of positive growth rate and the middle row of 0. Živjo :) – Vita Živa Alif Jun 17 '18 at 07:13
  • What is `format(pie.ch[1,1,1], digits = 16)`? How about `format(pie.ch[3,1,1], digits = 16)`? – De Novo Jun 17 '18 at 07:22
  • Please use line breaks, spaces and clearer formatting on your code to make it readable. – Elin Jun 17 '18 at 10:41
  • I made the formatting somewhat less brutal but it still needs work to improve readability. I'm also not sure you need all that code to provide a minimal example. – Elin Jun 17 '18 at 10:51
  • 1
    Then you must give us a way to create your array. Can you please edit the question with the output of `dput(pie.ch)` or if it is too big with a subset that can reproduce the problem? – Rui Barradas Jun 17 '18 at 16:40
  • The problem was in fact floating points and I managed to solve it. In terms of editing the question with `dput(pie.ch)` the output is not comprehensible, as an array of many matrices is transformed in a large chunk of numbers, therefore I would prefer not to edit the question with it – Vita Živa Alif Jun 18 '18 at 19:24

1 Answers1

1

Without a dput of your object, it's hard to say what your problem might be, but here's what I typically look for when a logical test for equality isn't meeting my expectations.

(x <- 0.5+0i + 1e-8)
# [1] 0.5+0i
Re(x) == 0.5
# [1] FALSE

The real portion of x is not, in fact, equal to 0.5. It just displays as if it's 0.5. You can examine this with as.character(x), which here will show "0.50000001+0i". To really drill down, try format(x, digits = 16).

(x <- 0.5+0i + .Machine$double.eps)
format(x, digits = 16)
# [1] "0.5000000000000002+0i"

As it is, though, your problem is not reproducible. Generating the array with the data you represented and running your code, pos is 0 and equal is 1 after executing.

pos <- 0
equal <- 0
pie.ch <- array(0, dim = c(3, 3, 3))
pie.ch[,,1] <- matrix(rep(c(0.5+0i, 0+0i, 0.5+0i), 3), 3)
pie.ch

# , , 1

#        [,1]   [,2]   [,3]
# [1,] 0.5+0i 0.5+0i 0.5+0i
# [2,] 0.0+0i 0.0+0i 0.0+0i
# [3,] 0.5+0i 0.5+0i 0.5+0i

...
i <- 1
if(Re(pie.ch[3,1,i])>Re(pie.ch[1,1,i])&&Re(pie.ch[3,1,i])>Re(pie.ch[2,1,i])){pos<-pos+1}
if((Re(pie.ch[1,1,i])==Re(pie.ch[2,1,i])&&Re(pie.ch[1,1,i])>Re(pie.ch[3,1,i]))||(Re(pie.ch[1,1,i])==Re(pie.ch[3,1,i])&&Re(pie.ch[1,1,i])>Re(pie.ch[2,1,i])||(Re(pie.ch[2,1,i])==Re(pie.ch[3,1,1])&&Re(pie.ch[3,1,i])>Re(pie.ch[1,1,i]))||Re(pie.ch[1,1,i])==Re(pie.ch[2,1,i])&&Re(pie.ch[2,1,i])==Re(pie.ch[3,1,i]))){equal<-equal+1}
pos
# [1] 0
equal
# [1] 1
De Novo
  • 7,120
  • 1
  • 23
  • 39