Search code examples
arraysmatlabexpandable

Matlab : expandable array, lost data


I have two columns of data. First column is time and second column is a function of time. However some time values are lost, so the function values. I don't know the index of the lost row (the amount of data is too large). For example, I have this:

 t   x+w
2t  2x+w
3t  3x+w
6t  6x+w
7t  7x+w

However, it should be like:

 t   x+w
2t  2x+w
3t  3x+w
4t  4x
5t  5x
6t  6x+w
7t  7x+w

I want to expand the time array and add the corresponding function values. Actually f(t) is random but have a linear growing deterministic behaviour. Thus, It does not matter if I add two values among the thousands. So how can I do this in Matlab?

Sorry for my English. I hope I could explain myself. Thanks.


Solution

  • If your data looks like this:

    t = [1 2 3 6 7];
    x = 2;
    w = 10;
    X = t*x + w;
    

    now you can just interpolate to get the missing X values:

    ti = 1:7;
    Xi = interp1(t, X, ti);
    

    or if you're saying you have this:

    t = [1 2 3 6 7];
    X = rand(size(t));
    

    then to fill in random values:

    Xi(t) = X; %Space out the origianl random value according to t
    Xi(setdiff(1:7,t)) = rand() %Find the missing vlaues using setdiff and replace them with new random values
    ti = 1:7;