r - Calculation of MSD with non uniform time intervals -
i have matrix has 2 columns x , y coordinates. want calculate mean square displacement - squared distance moved starting point point during given time, averaged on many different time points - assuming time intervals equal.
so working formula is:
msd=average(r(t)-r(0))^2 r(t) position @ time t , r(0) position @ time 0.
so code using calculate is:
#create vector save square of distance between successive #locations distsq<- numeric(length=nrow(mat)) #calculate , assign these values (i in 2:nrow(mat)) { distsq[i]<-((mat[i,1]-mat[i-1,1])^2)+((mat[i,2]-mat[i-1,2])^2) } #calculate mean sq distance value of n msd[k]<- mean(distsq) here mat matrix of x , y values.
so formula works when time between 2 successive points taken constant. suppose time between every 2 coordinates different, how can incorporate component calculate msd?
this should efficient (although roland partly correct in general claim of looping inefficiency.)
a <- matrix(1:20, ncol=2) mean( (a[,1] - a[1,1])^2 + (a[,2] - a[1,2])^2 ) [1] 57
Comments
Post a Comment