【问题标题】:Script in R returning numeric(0) and I cannot solveR中的脚本返回数字(0),我无法解决
【发布时间】:2016-08-12 04:44:21
【问题描述】:

我已经编写了一个脚本来计算笛卡尔坐标点矩阵中的线积分:

# Line Integral (Discrete)
# Input dataSet(as a matrix)
LineInt <- function(dataSet)
{
    Dist = 0;
    L = nrow(dataSet)
    for (idx in 1: L - 1)
     {
                    Dist = Dist +  (   ( (dataSet[idx + 1, 1] - dataSet[idx ,1])^2) + ((dataSet[idx + 1, 2] - dataSet[idx, 2])^2)  )^(1/2)
     }
    return(Dist)
}

脚本只需获取下一个点与实际点的差,计算距离的模数并存储在一个变量中,然后返回该变量。但由于某种原因,我得到所有输入的 numeric(0) 。我是初学者,在其他主题中仍然找不到解决方案。

【问题讨论】:

  • 我建议使用 R 代码样式(
  • 使用括号,例如1:(L-1)

标签: r for-loop integral


【解决方案1】:

idx 到 Dist 变量的索引应该做,希望你能得到解决方案

 LineInt <- function(dataSet)
    {
        Dist = 0;
        L = nrow(dataSet)
        for (idx in 1: (L - 1))
         {
           Dist[idx] = Dist +  (   ( (dataSet[idx + 1, 1] - dataSet[idx ,1])^2) + ((dataSet[idx + 1, 2] - dataSet[idx, 2])^2)  )^(1/2)
         }
        return(Dist)
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-06
    • 2022-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-14
    • 1970-01-01
    • 2022-11-23
    相关资源
    最近更新 更多