2012-06-05 70 views
1

我有一个有x和y坐标的2列的矩阵。我想计算均方位移 - 即在给定时间内从起点移动到另一点的平均距离,在许多不同时间点上平均 - 假设所有时间间隔相等。使用非均匀时间间隔计算MSD

所以工作公式为:

MSD=average(r(t)-r(0))^2 where r(t) is position at time t and r(0) is position at time 0.

因此,这我使用来计算这个代码是:

#Create a vector to save the square of the distance between successive 
#locations 
distsq<- numeric(length=nrow(mat)) 

#Calculate and assign these values 
for (i in 2:nrow(mat)) 
{ 
distsq[i]<-((mat[i,1]-mat[i-1,1])^2)+((mat[i,2]-mat[i-1,2])^2) 
} 

#Calculate the mean sq distance for this value of n 
MSD[k]<- mean(distsq) 

这里mat是x和y值的矩阵。

所以这个公式在两个连续点之间的时间被认为是恒定的时候起作用。但是,假设每两个坐标之间的时间不同,那么如何将该组件组合到一起来计算MSD?

+0

的不规则观察妥善处理可能取决于具体的应用。 –

+0

请注意,您可以使用的不仅仅是“point-point_before”信息。看看http://web.mit.edu/savin/Public/.Tutorial_v1.2/Concepts.html#A1 – dani

回答

0

首先,R中的循环非常缓慢。所以,出于性能原因,我会避免它并使用diff()

但是,您的实际问题是一个数学问题,如果没有更多的背景知识,很难回答。你可以使用某种加权函数:a(abs(deltat-b)),其中deltat是两点之间的时间差。

+0

'diff(a)'不等于'a [-1] -a [1]'第二个是r(t)-r(0)隐含的序列。 –

+0

给出的代码示例不执行前面介绍的内容。我会用diff()复制示例代码的输出。 – Roland

+0

确实如此,但是当回答有编码困难的人时,我通常会使用描述而不是他们的代码。 –

1

这应该是相当有效的(虽然罗兰只是他的循环效率低下的一般要求在部分正确。)

A <- matrix(1:20, ncol=2) 
mean((A[,1] - A[1,1])^2 + (A[,2] - A[1,2])^2) 
[1] 57