python中向量长度_python中不同长度点的平均多个向量

不幸的是没有人接受,这里有一个解决方案,我觉得行得通。在

我不得不改变数据的格式来解决这个问题。

因此,与其列出(x,y)点数可变的试验列表:

[[(x,y),(x,y),…],[(x,y),(…),…]]

我现在有3个numpy.arrays公司公司名称:

sx=数组([23,34,42,…,56,56,63])

sy=数组([78,94,20,…,44,38,34])

st=数组([1,1,1,…,293,293,293])

所有向量的长度都是一样的,因为它们本质上是一个表的一部分,其中sx是包含所有x位置的列,sy是所有y位置,st是试验编号(或x和y位置的列表ID)。st基本上是一组重复数[1,1,1,1,1,2,2,2,2,3,3,3,3,…]

(实际上,我使用HDF5/pytables来存储数据,它是从包含跟踪数据的表中直接读取的)

此解决方案使用interp1dfrom scipy.interpolate import interp1d

当然还有纽比

^{pr2}$

我承认这是一个黑客的解决方案,不是真的很快,但它的工作:)另一方面,重新阅读我自己的问题,使我认为这不是一个非常清楚的说明我的问题。。。对不起。不管怎样,这就是解决办法。在

下面的func接收我上面描述的3个vec,atrialList,它是一个要崩溃的试验列表,种类是你想要做的崩溃类型,现在可以是平均值或中位数。它将返回折叠轨迹,即平均值的x和y位置或三列曲线的中间值def collapseTrajectories(sx, sy, st, trialList, kind='median'):

# find the longest trial to use as template

l = 0

tr = []

for t in trialList:

if len(st[st==t]) > l:

l = len(st[st==t])

tr = t

# Make all vectors the same length by interpolating the values

xnew = np.linspace(0, 640, l)

ynew = np.linspace(0, 480, l)

sx_new = []

sy_new = []

for t in trialList:

if len(st[st==t]) > 3:

X = sx[st==t]

Y = sy[st==t]

x = np.linspace(0,640, len(X))

y = np.linspace(0,480,len(Y))

fx = interp1d(x, X, kind='cubic')

fy = interp1d(y, Y, kind='cubic')

sx_new.append(fx(xnew))

sy_new.append(fy(ynew))

# Collapse using the appropriate kind

if kind == 'median':

out_x = np.median(sx_new, axis=0)

out_y = np.median(sy_new, axis=0)

elif kind=='mean':

out_x = np.mean(sx_new, axis=0)

out_y = np.mean(sy_new, axis=0)

return out_x, out_y

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值