0

我正在尝试计算两个向量之间的角度,其中一个向量是固定的,另一个向量是不断移动的。我已经知道其中的数学,并且我之前找到了一个代码:

theta <- acos( sum(a*b) / ( sqrt(sum(a * a)) * sqrt(sum(b * b)) ) )

我尝试将我的 a 定义为:

a<-c(503,391)

我的 b 为:

b <- NM[, c("X","Y")]

当我应用 theta 函数时,我得到:

Warning message:
In acos(sum(a * b)/(sqrt(sum(a * a)) * sqrt(sum(b * b)))) : NaNs produced

我将不胜感激帮助解决这个问题。

这是我的示例数据:

structure(list(A = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label =
c("1",  "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12",
"13",  "14", "15", "16", "17", "18", "19", "20", "21", "22", "23",
"24",  "25", "26", "27", "28", "29", "30", "31", "32", "33", "34",
"35",  "36", "37", "38", "39", "40", "41", "42", "43", "44", "45",
"46",  "47", "48", "49", "50", "51", "52", "53", "54", "55", "56",
"57",  "58", "59", "60", "61", "62", "63", "64", "65", "66", "67",
"68",  "69", "70", "71", "72", "73", "74", "75", "76", "77", "78",
"79",  "80", "81", "82", "83", "84", "85", "86", "87", "88", "89",
"90",  "91", "92", "93", "94", "95", "96", "97", "98", "99", "100", 
"101", "102", "103", "104", "105", "106", "107", "108", "109", 
"110"), class = "factor"), T = c(0.1, 0.2, 0.3, 0.4, 0.5, 0.6 ), X =
c(528.04, 528.04, 528.04, 528.04, 528.04, 528.04), Y = c(10.32, 
10.32, 10.32, 10.32, 10.32, 10.32), V = c(0, 0, 0, 0, 0, 0), 
    GD = c(0, 0, 0, 0, 0, 0), ND = c(NA, 0, 0, 0, 0, 0), ND2 = c(NA, 
    0, 0, 0, 0, 0), TID = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = c("t1", 
    "t10", "t100", "t101", "t102", "t103", "t104", "t105", "t106", 
    "t107", "t108", "t109", "t11", "t110", "t12", "t13", "t14", 
    "t15", "t16", "t17", "t18", "t19", "t2", "t20", "t21", "t22", 
    "t23", "t24", "t25", "t26", "t27", "t28", "t29", "t3", "t30", 
    "t31", "t32", "t33", "t34", "t35", "t36", "t37", "t38", "t39", 
    "t4", "t40", "t41", "t42", "t43", "t44", "t45", "t46", "t47", 
    "t48", "t49", "t5", "t50", "t51", "t52", "t53", "t54", "t55", 
    "t56", "t57", "t58", "t59", "t6", "t60", "t61", "t62", "t63", 
    "t64", "t65", "t66", "t67", "t68", "t69", "t7", "t70", "t71", 
    "t72", "t73", "t74", "t75", "t76", "t77", "t78", "t79", "t8", 
    "t80", "t81", "t82", "t83", "t84", "t85", "t86", "t87", "t88", 
    "t89", "t9", "t90", "t91", "t92", "t93", "t94", "t95", "t96", 
    "t97", "t98", "t99"), class = "factor")), .Names = c("A",  "T", "X", "Y", "V", "GD", "ND", "ND2", "TID"), row.names = c(NA,  6L),
class = "data.frame")
4

2 回答 2

1

您的函数未矢量化。尝试这个:

theta <- function(x,Y) apply(Y,1,function(y,x) acos( sum(x*y) / ( sqrt(sum(x^2)) * sqrt(sum(y^2)) ) ),x=x)
a<-c(503,391)
b <- DF[, c("X","Y")]

theta(a,b)
#        1         2         3         4         5         6 
#0.6412264 0.6412264 0.6412264 0.6412264 0.6412264 0.6412264 
于 2013-07-01T11:04:49.600 回答
1

此应用程序中的 acos 和 atan 函数存在问题,因为您无法计算整个圆的角度,只能计算正象限的角度。在 2D 中,您需要两个值来指定一个向量,并且您需要两个值(sin 和 cos)来定义它,单位为度/弧度,最高为 2pi。以下是 acos 问题的示例:

plot(seq(1,10,pi/20))              ## A sequence of numbers
plot(cos(seq(1,10,pi/20)))         ## Their cosines
plot(acos(cos(seq(1,10,pi/20))))   ## NOT Back to the original sequence

这是一个想法:

angle <- circular::coord2rad(x, y)
plot(angle)

其中“(x,y)”具有“角度”

as.numeric(angle)

以弧度 (0,360) 给出角度. 要报告地理方向、转换为度数和其他内容,您可以使用为循环函数添加的参数,例如:

x <- coord2rad(ea,eo,  control.circular = list(type = "directions",units = "degrees"))
plot(x)
as.numeric(x)
于 2017-11-25T17:21:41.710 回答