Compare 2 Matrices and Return a Third (R)

Asked

Viewed 2,857 times

3

The following matrices being

Matrix(A)

     [,1]  [,2]  [,3]  [,4]  [,5]
[1,] 0.228 0.285 0.285 0.285 0.380
[2,] 0.228 0.285 0.570 0.380 0.228
[3,] 0.380 0.285 0.228 0.380 0.285
[4,] 0.285 0.285 0.570 0.380 0.380
[5,] 0.380 0.228 0.285 0.285 0.380

Matrix(B)

     [,1]  [,2]  [,3]  [,4]  [,5]
[1,] 0.383 0.174 0.535 0.700 0.396
[2,] 0.404 0.785 0.346 0.838 0.074
[3,] 0.591 0.554 0.260 0.229 0.361
[4,] 0.176 0.865 0.423 0.166 0.349
[5,] 0.132 0.018 0.456 0.684 0.150

How can I return a third Matrix(C) that returns 1 if the value at the same position as the Matrix(B) is less than or equal to Matrix(A) and 0 if it is larger ?

The Resulting Matrix(C) would be

      [,1]  [,2]  [,3]  [,4]  [,5]
[1,]  0     1     0     0     0
[2,]  0     0     1     0     1
[3,]  0     0     0     1     0
[4,]  1     0     1     1     1
[5,]  1     1     0     0     1
  • I ended up making a minimal example!

2 answers

3


Answer

C <- ifelse(B <= A, 1, 0)

Explanation

The operator <= is vectorized: it returns the comparison result (TRUE or FALSE) between corresponding elements of the vectors or matrices compared. Therefore, B <= A returns an array of TRUE and FALSE of the same size as A and of B. Upshot:

      [,1]  [,2]  [,3]  [,4]  [,5]
[1,]  TRUE FALSE  TRUE  TRUE  TRUE
[2,]  TRUE  TRUE FALSE  TRUE  TRUE
[3,]  TRUE  TRUE  TRUE FALSE  TRUE
[4,] FALSE  TRUE FALSE FALSE FALSE
[5,] FALSE FALSE  TRUE  TRUE FALSE

The function ifelse exchange the values TRUE of a vector or matrix by the second argument (in the example, 1) and the values FALSE by the third argument (in the example, 0).

Workaround

More economic but more esoteric solution:

C <- (B <= A) + 0

(sum is a trick to convert the boolean matrix to integer matrix)

  • 2

    Wow that cool, I’m not a programmer in R, congratulations ...

  • 2

    I think this is the most elegant and quick way to the problem.

  • 2

    Excellent, better than impossible.

  • Very very very good!

1

Coding:

# CRIAÇÃO DAS VARIAVEIS DE MATRIZ
A <- matrix(1:25, 5, 5);
B <- matrix(1:25, 5, 5);
C <- matrix(1:25, 5, 5);

# SETANDO OS VALORES DA MATRIZ A
A[1,1] = 0.228; A[1,2] = 0.285; A[1,3] = 0.285; A[1,4] = 0.285; A[1,5] = 0.380;
A[2,1] = 0.228; A[2,2] = 0.285; A[2,3] = 0.570; A[2,4] = 0.380; A[2,5] = 0.228;
A[3,1] = 0.380; A[3,2] = 0.285; A[3,3] = 0.228; A[3,4] = 0.380; A[3,5] = 0.285;
A[4,1] = 0.285; A[4,2] = 0.285; A[4,3] = 0.570; A[4,4] = 0.380; A[4,5] = 0.380;
A[5,1] = 0.380; A[5,2] = 0.228; A[5,3] = 0.285; A[5,4] = 0.285; A[5,5] = 0.380;


# SETANDO OS VALORES DA MATRIZ B
B[1,1] = 0.383; B[1,2] = 0.174; B[1,3] = 0.535; B[1,4] = 0.700; B[1,5] = 0.396;
B[2,1] = 0.404; B[2,2] = 0.785; B[2,3] = 0.346; B[2,4] = 0.838; B[2,5] = 0.380;
B[3,1] = 0.591; B[3,2] = 0.554; B[3,3] = 0.260; B[3,4] = 0.229; B[3,5] = 0.361;
B[4,1] = 0.176; B[4,2] = 0.865; B[4,3] = 0.423; B[4,4] = 0.166; B[4,5] = 0.349;
B[5,1] = 0.132; B[5,2] = 0.018; B[5,3] = 0.456; B[5,4] = 0.684; B[5,5] = 0.150;

i <- 1;
j <- 1;

#Como eu consigo retornar uma terceira 
#Matriz (C) que retorna 1 se o valor na mesma 
#posicao da matrix B for menor ou igual do que o da 
#matriz(A) e 0 se for maior ?

while(i < 6)
{
   j <- 1;
   while(j < 6){
      C[i,j] <- 1;
      if (A[i,j] <= B[i,j]){
         C[i,j] <- 0; 
      }
      j <- j + 1;
   }
   i <- i + 1;
}

#MOSTRANDO VALORES RESULTADOS DA MATRIZ C 
C;

Example: Ideone

References:

Browser other questions tagged

You are not signed in. Login or sign up in order to post.