point 如何求elbow_elbow 求拐点 - osc_44fs053n的个人空间 - OSCHINA - 中文开源技术交流社区...

本文介绍了如何使用距离点到线段的公式来计算数据集的肘部点(elbow point)。通过计算每个点到直线的最短距离,找到距离最大的点作为肘部点。首先定义了计算距离的函数,然后应用到数据集中,绘制距离分布并标识出肘部点,最后在图表上用垂直线标记出肘部点的位置。
摘要由CSDN通过智能技术生成

distancePointLine

## x, y is the point to test.

## slope, intercept is the line to check distance.

##

## Returns distance from the line.

##

## Returns 9999 on 0 denominator conditions.

x1

x2

y1

y2

distancePointSegment(x,y, x1,y1, x2,y2)

}

distancePointSegment

## px,py is the point to test.

## x1,y1,x2,y2 is the line to check distance.

##

## Returns distance from the line, or if the intersecting point on the line nearest

## the point tested is outside the endpoints of the line, the distance to the

## nearest endpoint.

##

## Returns 9999 on 0 denominator conditions.

lineMagnitude

ans

ix

lineMag

if( lineMag < 0.00000001) {

warning("short segment")

return(9999)

}

u

u

if((u < 0.00001) || (u > 1)) {

## closest point does not fall within the line segment, take the shorter distance

## to an endpoint

ix

iy

if(ix > iy)  ans

else ans

} else {

## Intersecting point is on the line, use the formula

ix

iy

ans

}

ans

}

###############################################

mydata = read.table('clipboard',header = T)

##########################

#mydata=cbind(c(1:15),wss)

#########################

datanumber = nrow(mydata)

mydist = c()

for(i in c(1:datanumber)){

d = as.numeric(c(mydata[i,],mydata[1,],mydata[datanumber,]))

mydist[i] = distancePointSegment(d[1],d[2],d[3],d[4],d[5],d[6])

}

mydist

max(mydist)

###############

#filternew = filter(mydist,filter = c(rep(1/datanumber,3)))

#plot(filternew)

################

elbowpoints = which(mydist==max(mydist))

plot(mydist)

abline(v=elbowpoints,lty=2,col='red')

plot(x=mydata[,1],y=mydata[,2],type='l')

elbowp = mydata[elbowpoints,1]

elbowp

abline(v=elbowp,lty=2,col='red')

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值