1
votes

Consider the following example:

time = [733774,733774,733775,733775,733775,733776,733776];
depth = [0,10,0,5,10,0,10];
d = [1,1.3,1,2.5,2.5,1,1.2];
data = horzcat(time',depth',d');

dz = 1;

Here I have a number of measurements taken for three separate days. The first day has 2 measurements taken at 2 separate depths, the second day has 3 measurements taken at 3 depths and the third has 2 measurements taken at 2 depths.

I would like to generate a new variable 'newData' which linearly interpolated (interp1) the values in data. However, I would only like to do this if the number of measurements for any given day exceeds 2. So, for the example above this would only apply to 733775. Here I would like to take the depth measurements and increase the vertical resolution e.g.

newDepth = min(depth):dz:max(depth);

But only perform this for the days when the number of measurements exceed 2. The outcome of what I have described should therefore be:

733774  0
733774  10
733775  0
733775  1
733775  2
733775  3
733775  4
733775  5
733775  6
733775  7
733775  8
733775  9
733775  10
733776  0
733776  10

plus the interpolated values from 'data(:,3)'.

What would be the best way of achieving this?

1
there's no depth 0 for 733775 in the output. Is that intentional? What if the depths are [1 3 1] for a given day? What if they are [1 1 4]? What if they are [1 4 5]? How should the interpolatio look in these cases? - Jonas
It should be an interpolation from the minimum depth to the maximum at intervals of 1. Also, the depths would not be [1 3 1] in my example. Corrected the example. Thanks - KatyB

1 Answers

1
votes

Here's one way to do this (solving the edited version of the OP this time):

[idx,~,uniqueTimes] = grp2idx(time);

counts = hist(time,uniqueTimes);


%# expand data with counts greater than minCount
minCount = 2;
dz = 1;

expandedData = mat2cell(data,counts,3)

for ii = find(counts>minCount)'
    minZ = expandedData{ii}(1,2);
    maxZ = expandedData{ii}(end,2);

    intData(:,2) = (minZ:dz:maxZ)';
    intData(:,1) = expandedData{ii}(1,1);
    intData(:,3) = interp1(expandedData{ii}(:,2),expandedData{ii}(:,3),intData(:,2));

    expandedData{ii} = intData;

end

out = cat(1,expandedData{:})