Calculating mean-squared displacement (msd) with MATLAB

前端 未结 2 1218
一生所求
一生所求 2020-12-18 09:22

This is part of a matlab intro course for biologists. I have my data points (for a single particle!) in a matrix with 4 columns (time, x, y, z) and a few thousand rows. What

相关标签:
2条回答
  • 2020-12-18 10:01

    Note that there's a MATLAB class available to do MSD analysis: MSD analyzer, including extensive documentation and tutorials.

    Basic usage:

    ma = msdanalyzer(2, SPACE_UNITS, TIME_UNITS);
    ma = ma.addAll(tracks);
    ma = ma.computeMSD;
    
    0 讨论(0)
  • 2020-12-18 10:02

    If you're calculating the displacement only with respect to the first position, then you don't actually average anything, since MSD(1) is the average distance your object travels within one time step. So your reasoning is spot on.

    However, I would calculate MSD the other way around: Get all displacements at dt=1 (1-2,2-3,3-4,...) and average. This is MSD(1). Then, you get all displacements at dt=2 (1-3,2-4,...) and average. This is MSD(2). And so on.

    A useful property of Matlab is that you can vectorize some computations, i.e. do the calculation on an entire array, rather than doing them element-by-element. So if you have an array a of 100-by-1 coordinates, the difference between each coordinate and the next one is b=a(2:100)-a(1:99), or more generally, b=a(2:end)-a(1:end-1), so b(1) is a(2)-a(1), b(2) is a(3)-a(2) etc.

    To calculate MSD from your array data (where I assume that time is in equal steps!), you'd write

    nData = size(data,1); %# number of data points
    numberOfdeltaT = floor(nData/4); %# for MSD, dt should be up to 1/4 of number of data points
    
    msd = zeros(numberOfDeltaT,3); %# We'll store [mean, std, n]
    
    %# calculate msd for all deltaT's
    
    for dt = 1:numberOfDeltaT
       deltaCoords = data(1+dt:end,2:4) - data(1:end-dt,2:4);
       squaredDisplacement = sum(deltaCoords.^2,2); %# dx^2+dy^2+dz^2
    
       msd(dt,1) = mean(squaredDisplacement); %# average
       msd(dt,2) = std(squaredDisplacement); %# std
       msd(dt,3) = length(squaredDisplacement); %# n
    end
    
    0 讨论(0)
提交回复
热议问题