Search code examples
matlabfunctionhistogramdistributionmotion

Matlab - Cumulative Distribution of trajectory


To the best of my knowledge,I did not find any answers anywhere to the answer to my problem. I consider myself pretty decent in Matlab.

I have the trajectory of a tumor recorded vs time like on this image. I would like to compute cumulative distribution that would show the time vs displacement of the tumor from its ideal position at x=0 like on this picture generated with another software.

What the cumulative graph means is that we can find the total time outside a certain position the tumor spent during acquisition. You see that the position of the tumor at the position 0 is the length of the entire acquisition time(~300 seconds).If we are looking for the time the tumor spent outside 1.1 mm from their ideal position, it would indicate ~100 seconds.The time the tumor spent outside 2.8mm becomes really close to 0s.

Any code that would help me get such a would be great. I strongly sense that there is something to do with cumsum, cdf etc, but I really have not managed to find a proper function. My next option would be to bin it myself and write the code for it.

Thank you for you help.


Solution

  • You can find the distribution using hist(x). Then by calculating the backwards cumsum() you can draw the desired plot.

    clc, clear all, close all
    seconds = 303;              % Amount of time that passed during the test
    datapoints = 3000;          % Amount of Datapoints in your vector 
    
    x = randn(datapoints,1);
    [counts,centers] = hist(abs(x),sort([0;unique(abs(x))]));
    
    sumX = sum(counts);
    cumsumX = cumsum(counts);
    time = (sumX - [0 cumsumX(1:end-1)])*seconds/datapoints; % Normalize result with factor
    
    figure
    plot(centers, time)