# Calculating with different date times

43 views (last 30 days)
Christopher on 27 May 2024 at 16:36
Edited: Peter Perkins on 28 May 2024 at 20:36
Hi,
I have one array that contains my speed during running and the corresponding time it was recorded at. Measurements are about every one second.
Then I have a second array that contains my heart rate measurements and the corresponding time they were recorded at. These measurements contain data from about every 6-10 seconds.
This is shown by the exemplary screenshot.
What I am trying to do is divide my speed by heart rate at the corresponding times to get something like 'performance'. So I need to average the speed of all measurements that were done for one measurement of heart rate. Something like this:
mean(Speed(1:6))/Heartrate(1)
mean(Speed(7:16))/Heartrate(2)
I cannot do this by hand for the amount of data but have problems coming up with code that selects the correct time range to average the speed (the time range that corresponds to every one measurement of heart rate) and then divides these values.
PS Sorry for the bad question title, I couldn't think of a better one :/
Ayush Modi on 27 May 2024 at 18:29
Edited: Ayush Modi on 27 May 2024 at 18:29
Hi Christopher,
Try this ->
% I have assumed the names of arrays as below
speed_time = duration(15, 15, 24:45);
speed_values = [3, 4, 5, 3, 4, 5, 3, 4, 5, 3, 3, 4, 5, 6, 7, 5, 3, 4, 3, 3, 2, 2]; % I extended the array from the image to show the result
hr_time = duration(15, 15, [24, 30, 39]);
hr_values = [143, 145, 134];
performance = [];
for i = 1:length(hr_time)-1
indices = find(speed_time >= hr_time(i) & speed_time < hr_time(i+1));
mean_speed = mean(speed_values(indices));
performance = [performance, mean_speed / hr_values(i)];
end
% Handling the last heart rate measurement
indices = find(speed_time >= hr_time(end));
mean_speed = mean(speed_values(indices));
performance = [performance, mean_speed / hr_values(end)];
disp(performance);
0.0280 0.0307 0.0235

Mario Malic on 27 May 2024 at 18:31
Edited: Mario Malic on 27 May 2024 at 18:33
Hello Christopher,
Here's the code that does it
speedTime = datetime("now");
speedTime = dateshift(speedTime, "start", "second", 0:15);
speed = 1:numel(speedTime);
speedTable = table(speedTime', speed');
speedTable.Properties.VariableNames = {'Time', 'Speed'};
heartTime = datetime("now");
heartTime = dateshift(heartTime, "start", "second", 0:5:15);
bpm = linspace(130, 150, numel(heartTime));
heartTable = table(heartTime', bpm');
heartTable.Properties.VariableNames = {'Time', 'BPM'};
Time Speed ____________________ _____ 27-May-2024 18:33:05 1 27-May-2024 18:33:06 2 27-May-2024 18:33:07 3 27-May-2024 18:33:08 4 27-May-2024 18:33:09 5 27-May-2024 18:33:10 6 27-May-2024 18:33:11 7 27-May-2024 18:33:12 8
Time BPM ____________________ ______ 27-May-2024 18:33:05 130 27-May-2024 18:33:10 136.67 27-May-2024 18:33:15 143.33 27-May-2024 18:33:20 150
for i = 1 : height(heartTable) - 1
idxFrame = isbetween(speedTable.Time, heartTable.Time(i), heartTable.Time(i + 1)); % gets indices between i-th and i+1-th time
if any(idxFrame) % There is overlap in time between two sensors
performance = mean(speedTable.Speed(idxFrame)) / heartTable.BPM(i)
else
error("measurement error")
end
end
performance = 0.0269
performance = 0.0622
performance = 0.0942

Peter Perkins on 28 May 2024 at 20:35
Edited: Peter Perkins on 28 May 2024 at 20:36
I would think you would want to compute performance at each speed measurement by interpolating heart rate, rather than computing at each heart rate measurement by averaging speed. In any case, either becomes easy with a timetables and retime or synchronize.
Time = datetime(2024,5,28,16,30,0) + seconds(0:30)';
Speed = (1:numel(Time))';
speedTable = timetable(Time,Speed)
speedTable = 31x1 timetable
Time Speed ____________________ _____ 28-May-2024 16:30:00 1 28-May-2024 16:30:01 2 28-May-2024 16:30:02 3 28-May-2024 16:30:03 4 28-May-2024 16:30:04 5 28-May-2024 16:30:05 6 28-May-2024 16:30:06 7 28-May-2024 16:30:07 8 28-May-2024 16:30:08 9 28-May-2024 16:30:09 10 28-May-2024 16:30:10 11 28-May-2024 16:30:11 12 28-May-2024 16:30:12 13 28-May-2024 16:30:13 14 28-May-2024 16:30:14 15 28-May-2024 16:30:15 16
Time = datetime(2024,5,28,16,30,0) + seconds(0:5:30)';
BPM = linspace(130, 150, numel(Time))';
heartTable = timetable(Time,BPM)
heartTable = 7x1 timetable
Time BPM ____________________ ______ 28-May-2024 16:30:00 130 28-May-2024 16:30:05 133.33 28-May-2024 16:30:10 136.67 28-May-2024 16:30:15 140 28-May-2024 16:30:20 143.33 28-May-2024 16:30:25 146.67 28-May-2024 16:30:30 150
Version 1: average the speed every 5 seconds
data = synchronize(speedTable,heartTable,"last","mean");
data.Performance = data.Speed ./ data.BPM
data = 7x3 timetable
Time Speed BPM Performance ____________________ _____ ______ ___________ 28-May-2024 16:30:00 3 130 0.023077 28-May-2024 16:30:05 8 133.33 0.06 28-May-2024 16:30:10 13 136.67 0.095122 28-May-2024 16:30:15 18 140 0.12857 28-May-2024 16:30:20 23 143.33 0.16047 28-May-2024 16:30:25 28 146.67 0.19091 28-May-2024 16:30:30 31 150 0.20667
Version 2: Interpolate the BPM at each second
data = synchronize(speedTable,heartTable,"first","spline");
data.Performance = data.Speed ./ data.BPM
data = 31x3 timetable
Time Speed BPM Performance ____________________ _____ ______ ___________ 28-May-2024 16:30:00 1 130 0.0076923 28-May-2024 16:30:01 2 130.67 0.015306 28-May-2024 16:30:02 3 131.33 0.022843 28-May-2024 16:30:03 4 132 0.030303 28-May-2024 16:30:04 5 132.67 0.037688 28-May-2024 16:30:05 6 133.33 0.045 28-May-2024 16:30:06 7 134 0.052239 28-May-2024 16:30:07 8 134.67 0.059406 28-May-2024 16:30:08 9 135.33 0.066502 28-May-2024 16:30:09 10 136 0.073529 28-May-2024 16:30:10 11 136.67 0.080488 28-May-2024 16:30:11 12 137.33 0.087379 28-May-2024 16:30:12 13 138 0.094203 28-May-2024 16:30:13 14 138.67 0.10096 28-May-2024 16:30:14 15 139.33 0.10766 28-May-2024 16:30:15 16 140 0.11429
You may choose to bin things differently at the edges, but that's the general idea.