Иметь верхнюю матрицу tri angular из нижней матрицы tri angular с R - PullRequest
0 голосов
/ 16 апреля 2020

У меня есть вектор, который состоит из данных из более низкой матрицы angular.

m_lower <- c(0.3663761172,0.4863082351,0.7465202620,0.4965009484,0.0749920755,4.4082686453,3.2714448866,0.7604404092,1.5994592726,0.2677065512,1.4247358706,1.8448569996,1.9888525802,0.6684931466,0.0909124206,1.2443815645,0.1329758523,0.4777177616,4.7059557222,0.0018111412,3.2430708925,1.7024842083,0.2973714654,1.8145898881,0.8277291485,0.4898066476,0.2827415558,0.2652730958,0.4801885476,1.9012667391,4.2655464241,0.6021593916,0.7127273433,3.6781491268,0.2084725974,0.3147488236,0.0977461927,0.1689097181,0.2176950021,0.0114681239,0.2621692606,0.1242180116,0.0530288130,0.0065052254,0.1241324065,0.3803137139,0.3877225011,0.1456193524,0.0238036494,0.6558033727,0.8803106660,0.0846636279,0.0375347721,0.4605712015,4.7307220442,0.5978980924,4.9502297322,1.6783343118,0.1872433954,0.0096240780,2.5275913377,0.7891683399,0.2747600533,0.8053761872,0.1541668145,0.2320415088,0.9950147294,0.5487573930,0.4876815384,0.0389633056,0.9807247107,2.6210927047,0.1429777740,0.1381647168,0.5751212069,3.1234289486,6.2097230541,0.6347384457,0.2290039612,0.1133552059,0.1790568649,0.0375669532,0.8222290651,0.1216843377,0.0296845741,0.1042203024,1.8920649062,1.3812280928,2.7648752363,0.0506352673,1.9935626852,1.4874099504,0.2729502243,0.2616840959,0.2617474854,0.0813626319,0.5760642438,0.3235973298,0.2360780322,0.4039630140,0.1098278657,0.2043569205,0.3537233928,0.1890846579,0.1392357526,5.5852889211,0.8895464606,4.4717950139,1.4398239163,4.1612760128,1.5346225800,0.6834217952,1.7567290546,1.1563462407,0.0455001087,0.1562431558,0.8705330892,0.5275899123,0.3237870293,1.2863038275,1.6973196296,0.5847098190,2.3223925288,0.5613444343,2.2124772293,1.4562826709,0.8400955943,0.1424881993,0.6683791808,0.8525094744,0.3708594594,1.1009684274,1.5736877623,0.2093061150,0.5452534503,7.3930448949,0.1652496048,0.8540872055,0.1483290331,0.0281819105,0.7922539949,0.2942266303,0.0628311599,0.1691295505,2.4279363796,0.1212368185,0.5582404808,0.1328161192,1.0350547406,5.7389081114,0.1187634510,0.2655712856,0.2854689127,0.1898174381,0.3563226261,0.5320306856,0.1195664207,0.9398889664,0.3591286333,0.0931564303,0.0782904570,5.1694252396,0.3460867771,0.4119580757,0.1127626303,0.6218762663,13.2700188970,0.1119347335,0.4291885095,0.3251606306,4.7433868446,1.9050191397,0.1906132162,0.1333666643,0.0419028810,2.6071857096,0.3753732407,0.3117898454,0.0429600236,0.1713773435,10.7412818563,1.7645514498,0.2214461620,1.7688809751,0.6670375727,0.3626269616,0.0956151063,2.0777759737,0.1852685974,0.3278837993)

Нижняя матрица изображения

Как я могу получить всю матрицу или верхнюю матрицу tri angular из моего вектора, которая состоит из данных из нижней матрицы tri angular?

Ответы [ 2 ]

1 голос
/ 16 апреля 2020

Понятия не имею, если вы этого хотите

n <- (sqrt(1+8*length(m_lower))+1)/2
m <- matrix(nrow = n,ncol = n)
m[lower.tri(m)] <- m_lower
1 голос
/ 16 апреля 2020

Вы можете использовать lower.tri и upper.tri, чтобы добиться этого чем-то вроде:

# Create some equivalent, but smaller toy data
n <- 5
X <- matrix(rnorm(n^2), n, n) # Initialize empty matrix
m <- (t(X) + X)/2   # Make symmetric "m"
m_lower <- m[lower.tri(m, diag = TRUE)]
m_lower
# [1] -0.475060360  0.439727314 -0.332462307  0.628518993 -1.528984770 -0.115709110 -1.172260961
# [8]  0.006313545  0.018025707  0.016831925  0.228239570  1.450523622 -0.539834781 -0.152822864
#[15] -0.522060544

# Determine dimension (if square and diagonal included in m_lower)
l <- length(m_lower)
n <- (sqrt(1 + 8*l) - 1)/2  # Solve n*(n + 1) = l for n

# Reconstruct 
m2 <- matrix(NA, n, n)
m2[lower.tri(m2, diag = TRUE)] <- m_lower
m2[upper.tri(m2)] <- t(m2)[upper.tri(m2)]  # If symmetric, fill also upper half

# Check
all(m == m2)
# [1] TRUE

Редактировать: теперь обернуто в функцию, которая принимает дополнительные аргументы, если восстановление должно быть симметричным c и / из, если диагональ включена в m_lower (см. ответ от ThomasIsCoding).

# Create function for reconstruction
reconstruct <- function(m_lower, diag = TRUE, symmetric = TRUE) {
  # Determine dimension (if square and diagonal included in m_lower)
  l <- length(m_lower)
  n <- (sqrt(1 + 8*l) + ifelse(diag, -1, 1))/2  # Solve n*(n + 1) = l for n
  m <- matrix(NA, n, n)

  # Reconstruct
  m[lower.tri(m, diag = diag)] <- m_lower
  if (symmetric) { # If symmetric, fill also upper half
    m[upper.tri(m)] <- t(m)[upper.tri(m)]  
  }
  return(m)
}

m3 <- reconstruct(m_lower)

# Check
all(m == m3)
# [1] TRUE
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...