Compare 2 Matrices and Return a Third (R)

3

The following arrays

Matrix

     [,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

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

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
    
asked by anonymous 01.07.2014 / 16:38

2 answers

3

Response

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

Explanation

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

      [,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 ifelse function changes the TRUE values of a vector or array by the second argument (in the example, 1 ) and the FALSE values by the third argument (in the example, 0 ). p>

Workaround

More economical but more esoteric solution:

C <- (B <= A) + 0

(the sum is a trick to convert the array of booleans to array of integers)

    
02.07.2014 / 02:43
1

Encoding:

# 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:

01.07.2014 / 18:18