Calculating with different date times
4 次查看(过去 30 天)
显示 更早的评论
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.
I appreciate your ideas/ help!
PS Sorry for the bad question title, I couldn't think of a better one :/
2 个评论
Ayush Modi
2024-5-27
编辑:Ayush Modi
2024-5-27
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);
回答(2 个)
Mario Malic
2024-5-27
编辑:Mario Malic
2024-5-27
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'};
head(speedTable)
head(heartTable)
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
0 个评论
Peter Perkins
2024-5-28
编辑:Peter Perkins
2024-5-28
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)
Time = datetime(2024,5,28,16,30,0) + seconds(0:5:30)';
BPM = linspace(130, 150, numel(Time))';
heartTable = timetable(Time,BPM)
Version 1: average the speed every 5 seconds
data = synchronize(speedTable,heartTable,"last","mean");
data.Performance = data.Speed ./ data.BPM
Version 2: Interpolate the BPM at each second
data = synchronize(speedTable,heartTable,"first","spline");
data.Performance = data.Speed ./ data.BPM
You may choose to bin things differently at the edges, but that's the general idea.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!