How to find the linear regression of two data sets with different times and different size?

32 views (last 30 days)
Kofial on 6 Nov 2020
Commented: Kofial on 11 Nov 2020
I have two data sets:
A = [27 30 28 32 30] 1x5 double
that is measured during the time
Time_A=[00:38:00 00:39:00 00:40:00 00:41:00 00:42:00] 1x5 double
And B =[27 28 32 38 28 29 30 32 ] 1x8 double
measured during the time
Time_B=[00:40:30 00:41:30 00:42:30 00:43:30 00:43:30 00:44:30 00:45:30] 1x8 double
How can I find the linear regression? They have different size and the time doesn't match because they are collected from different instruments.
Thank you

Accepted Answer

Tim on 6 Nov 2020
It's not entirely clear what you are asking: are you asking how to perform a linear regression on the dataset formed by combining both sets of measurements and times? (By the way: it looks like there are only seven values in your second set of time stamps.) If so, just combine the time and measurement vectors and use Matlab's "polyfit" like in the following example. I'm making "A" your measurements from instrument 1 and AT your time stamps from set 1, and "B" your measurements from instrument 2 and BT the time stamps from instrument 2:
% Time vector, set 1
AT = 38:42;
% Measurements set 1 (simulated)
A = randn(size(AT)) + 0.1*(38:42); % Noise plus some slope
% Time vector, set 2
BT = (40:47) + 0.5;
% Measurements set 2 (simulated)
B = randn(size(BT)) + 0.1*(40:47); % Noise plus same slope
% Combine measurements and times
measurements = [A, B];
times = [AT, BT];
% Plot
plot(times, measurements, 'k.');
% Linear regression
P = polyfit(times, measurements, 1);
% Plot result
hold on;
plot(times, polyval(P, times), 'k');
hold off;
P has your regression coefficients, P(1) will be the slope (should be about ~0.1, gets better with more measurements) and P(2) should be the offset.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by