【问题标题】:Fail to fill in a triangular part of my matrix with a vector: warning about length无法用向量填充矩阵的三角形部分:关于长度的警告
【发布时间】:2018-12-26 05:24:38
【问题描述】:

我有一个向量要填充到矩阵的下三角部分。但我收到了一条警告信息。

par1 <-  0.5:7
par1 <- sample(par1, 496, replace = T)
par11 <- matrix(0, 32, 32)
par11[lower.tri(par11, diag = TRUE)] <- par1

Warning message:
In par11[lower.tri(par11, diag = TRUE)] <- par1 :
  number of items to replace is not a multiple of replacement length

我的矩阵的维度是32,所以我应该有496 非零元素。我的错误在哪里?

【问题讨论】:

  • 当您添加参数 diag = T 时,长度会从 496 变为 496+32=528。因此不等向量。你不使用 diag=T 或者你可以采样 528

标签: r matrix


【解决方案1】:

N x N 矩阵中的三角形部分(没有主对角线)具有N x (N - 1) / 2 元素。如果包括主对角线,则有N x (N + 1) / 2

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-23
    • 1970-01-01
    • 1970-01-01
    • 2014-11-21
    • 2015-01-13
    • 1970-01-01
    相关资源
    最近更新 更多