问题
Question: I am dealing with a patient data, where parameters are recorded at different sampling frequency, so having a different time stamp.
I want to create a matrix where data is interpolated by "Last known value" till the new original value changes in the time. So at the end I have uniform matrix where each parameter have values at every time stamp.
Data is in following format:
Time Hear Rate(Variable)
18:00:00 PM 74
18:02:00 PM 75
18:04:00 PM 85
18:06:00 PM 71
18:08:00 PM 79
18:10:00 PM 72
Time Blood Press. (Variable)
18:01:00 PM 100
18:05:00 PM 120
18:09:00 PM 121
Target :
Time Hear Rate(Variable) Blood Press.
18:00:00 PM 74 NaN
18:01:00 PM 74 100
18:02:00 PM 75 100
18:03:00 PM 75 100
18:04:00 PM 85 100
18:05:00 PM 85 120
18:06:00 PM 71 120
18:07:00 PM 71 120
18:08:00 PM 79 120
18:09:00 PM 79 121
18:10:00 PM 72 121
The Interpolated data in the missing place should be the previous value of the known event and should remain same until next change occurs.
I am currently referring the following thread from MATLAB User Forum
http://www.mathworks.com/matlabcentral/answers/101237-how-can-i-do-1-d-interpolation-with-interp1-to-find-the-nearest-value-to-the-left-of-the-point-i-e
回答1:
You're essentially trying to do a zero-order hold as you've seen from the link you provided.
Let's call the first set of times t_hr (a cell array of strings) and the second set t_bp (also a cell array of strings). Then call the heart rates hr and the blood pressures bp.
Build a new cell array t by combining t_hr and t_bp:
t = [t_hr; t_bp];
Fill in hr and bp with some NaNs to make them the same length as t. In t, the first part of the vector corresponds to times we know about for hr, and the second half corresponds to bp. Use that knowledge accordingly:
hr = [hr; nan(length(t) - length(hr),1)];
bp = [nan(length(t) - length(bp),1); bp];
Now you've got three vectors: t (okay, that's actually a cell array); hr and bp, and the elements of hr and bp correspond to elements in t, including the NaNs:
t =
'18:00:00 PM'
'18:02:00 PM'
'18:04:00 PM'
'18:06:00 PM'
'18:08:00 PM'
'18:10:00 PM'
'18:01:00 PM'
'18:05:00 PM'
'18:09:00 PM'
hr =
74
75
85
71
79
72
NaN
NaN
NaN
bp =
NaN
NaN
NaN
NaN
NaN
NaN
100
120
121
Now, we can sort t:
[t_sorted, idx] = sort(t);
idx contains the indices of t that got moved around to form t_sorted. In this case, idx == [1 7 2 3 8 4 5 9 6].'. We can use that to sort bp and hr:
hr_sorted = hr(idx)
hr_sorted =
74
NaN
75
85
NaN
71
79
NaN
72
bp_sorted = bp(idx)
bp_sorted =
NaN
100
NaN
NaN
120
NaN
NaN
121
NaN
Then, apply the zero-order hold:
for ii = 2:length(t)
if isnan(hr_sorted(ii))
hr_sorted(ii) = hr_sorted(ii-1);
end
if isnan(bp_sorted(ii))
bp_sorted(ii) = bp_sorted(ii-1);
end
end
and your final vectors become:
hr_sorted =
74
74
75
85
85
71
79
79
72
bp_sorted =
NaN
100
100
100
120
120
120
121
121
Note that your target answer has 11 different times in it, but you only supplied nine. (It's missing 18:03 and 18:07.) You can easily extend this answer by adding extra NaNs to hr and bp, and making t a cell array comprising of, say, t_hr, t_bp, and a cell array t_missing ={'18:03:00 PM', '18:07:00 PM'}.
来源:https://stackoverflow.com/questions/24083490/interpolate-the-time-stamped-patient-data