Mostrando entradas con la etiqueta matrix. Mostrar todas las entradas
Mostrando entradas con la etiqueta matrix. Mostrar todas las entradas

2020-12-21

How to create a symmetric matrix in R

Title

Problem

We want to create a symmetric matrix based on the following example.

Example

  V1 V2 V3 V4 V5
1  0  2  3  4  5
2  0  0  6  8 10
3  0  0  0 12 15
4  0  0  0  0 20
5  0  0  0  0  0
df <- structure(list(V1 = c(0L, 0L, 0L, 0L, 0L), V2 = c(2L, 0L, 0L, 
0L, 0L), V3 = c(3L, 6L, 0L, 0L, 0L), V4 = c(4L, 8L, 12L, 0L, 
0L), V5 = c(5L, 10L, 15L, 20L, 0L)), .Names = c("V1", "V2", "V3", 
"V4", "V5"), class = "data.frame", row.names = c("1", "2", "3", 
"4", "5"))

Solution

  • Adding the transpose of the matrix.
  • df + t(df)
  • Function lower.tri
  • We assign to the lower triangle of the matrix the transpose of the lower triangle.

    df[lower.tri(df)] <- t(df)[lower.tri(df)]
      V1 V2 V3 V4 V5
    1  0  2  3  4  5
    2  2  0  6  8 10
    3  3  6  0 12 15
    4  4  8 12  0 20
    5  5 10 15 20  0

    Using upper.tri would not create a symmetrical matrix.

    df[lower.tri(df)] <- df[upper.tri(df)]
      V1 V2 V3 V4 V5
    1  0  2  3  4  5
    2  2  0  6  8 10
    3  3  8  0 12 15
    4  6 12 10  0 20
    5  4  5 15 20  0
    

References

2015-09-28

Cómo crear una matriz simétrica en R

Title

Problema

Partimos de la siguiente matriz/data frame y deseamos crear una matriz simétrica.

Datos

  V1 V2 V3 V4 V5
1  0  2  3  4  5
2  0  0  6  8 10
3  0  0  0 12 15
4  0  0  0  0 20
5  0  0  0  0  0
df <- structure(list(V1 = c(0L, 0L, 0L, 0L, 0L), V2 = c(2L, 0L, 0L, 
0L, 0L), V3 = c(3L, 6L, 0L, 0L, 0L), V4 = c(4L, 8L, 12L, 0L, 
0L), V5 = c(5L, 10L, 15L, 20L, 0L)), .Names = c("V1", "V2", "V3", 
"V4", "V5"), class = "data.frame", row.names = c("1", "2", "3", 
"4", "5"))

Soluciones

  • Sumando la matriz transpuesta
  • df + t(df)
  • Función lower.tri.
  • df[lower.tri(df)] <- t(df)[lower.tri(df)]
      V1 V2 V3 V4 V5
    1  0  2  3  4  5
    2  2  0  6  8 10
    3  3  6  0 12 15
    4  4  8 12  0 20
    5  5 10 15 20  0

    Asignamos a la parte inferior de una matriz triangular superior, los valores de la parte superior. Con lower.tri obtenemos el triángulo inferior. Le asignamos la parte superior que transponemos previamente la matriz —t(df)—, por eso volvemos a usar lower.tri . Pues si usáramos upper.tri el resultado no sería una matriz simétrica.

    df[lower.tri(df)] <- df[upper.tri(df)]
      V1 V2 V3 V4 V5
    1  0  2  3  4  5
    2  2  0  6  8 10
    3  3  8  0 12 15
    4  6 12 10  0 20
    5  4  5 15 20  0
    

Entradas relacionadas

Referencias

2015-05-27

Project Euler - Problema 5 en R

Title Continuamos con los problemas planteados en Project Euler.

Problema - Smallest multiple

2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

2520 es el número más pequeño que puede ser dividido por cada uno de los números del 1 al 10 sin ningún resto.
¿Cuál es el número positivo más pequeño que es divisible por todos los números del 1 al 20?

Solución

require(numbers)
mLCM(1:20)
[1] 232792560

Notas

Usamos la función mLCM para calcular el mínimo común múltiplo de los números del 1 al 20.

Otra alternativa manual sería calcular los factores primos de cada uno de los números. Después inspeccionamos visualmente los factores comunes de todos ellos.

x <- matrix(1:20)
apply(x, 1, primeFactors)
[[1]]
[1] 1

[[2]]
[1] 2

[[3]]
[1] 3

[[4]]
[1] 2 2

[[5]]
[1] 5

[[6]]
[1] 2 3

[[7]]
[1] 7

[[8]]
[1] 2 2 2

[[9]]
[1] 3 3

[[10]]
[1] 2 5

[[11]]
[1] 11

[[12]]
[1] 2 2 3

[[13]]
[1] 13

[[14]]
[1] 2 7

[[15]]
[1] 3 5

[[16]]
[1] 2 2 2 2

[[17]]
[1] 17

[[18]]
[1] 2 3 3

[[19]]
[1] 19

[[20]]
[1] 2 2 5
Los factores serían: (2^4)*(3^2)*5*7*11*13*17*19 = 232792560

Para calcular los factores primos únicos

unique(unlist(apply(x, 1, primeFactors)))
[1]  1  2  3  5  7 11 13 17 19

Referencias

Nube de datos