Problem 652. Put two time series onto the same time basis
Use interpolation to align two time series onto the same time vector.
This is a problem that comes up in Trendy whenever you want to do mathematical operations on two trends (see for example the Ramen-to-Sushi Index).
Given two time series defined by the vector pairs (t1,d1) and (t2,d2), come up with a single new time vector tn such that (tn,d1n) and (tn,d2n) form consistent vector pairs.
Specifically, the new time vector should extend only to portions covered by both series (i.e. no extrapolation). Within this common interval, tn should contain all the time points from both time vectors. With this new time basis, use linear interpolation to generate two new data series.
Example 1:
t1 = [1 3 5] d1 = [2 4 2] t2 = [2 4] d2 = [1 5]
tn = [2 3 4] d1n = [3 4 3] d2n = [1 3 5]
Example 2:
t1 = [1 2 3 4 5] d1 = [2 3 5 6 3] t2 = [2 3.5 4 4.5 5 6] d2 = [3 9 8 2 0 1]
tn = [2 3 3.5 4 4.5 5] d1n = [3 5 5.5 6 4.5 3] d2n = [3 7 9 8 2 0]
Solution Stats
Problem Comments
-
3 Comments
neat! i'd never used time series before
I use timeseries all the time but don't usually use objects. Obviously using object oriented commands is much faster than more manual methods.
https://www.mathworks.com/matlabcentral/cody/problems/44699-prime-product
I created this problem. I think it should be in Indexing I.
Solution Comments
Show commentsGroup

Indexing I
- 27 Problems
- 236 Finishers
- Piecewise linear interpolation
- Longest run of consecutive numbers
- Construct an index vector from two input vectors in vectorized fashion
- Getting the indices from a matrix
- First non-zero element in each column
- Return unique values without sorting
- Return a list sorted by number of consecutive occurrences
- intersection of matrices
- Generate N equally spaced intervals between -L and L
- Check if number exists in vector
- Replicate elements in vectors
- We love vectorized solutions. Problem 1 : remove the row average.
- intersection of matrices
- Generate N equally spaced intervals between -L and L
- Create logical matrix with a specific row and column sums
- Return a list sorted by number of consecutive occurrences
- Replicate elements in vectors
- Get the elements of diagonal and antidiagonal for any m-by-n matrix
- Getting the indices from a matrix
- Check if number exists in vector
- Fill a zeros matrix
- Set the array elements whose value is 13 to 0
- Construct an index vector from two input vectors in vectorized fashion
- Joining Ranges
- Remove the two elements next to NaN value
- Reindex a vector
- Longest run of consecutive numbers
- Put two time series onto the same time basis
- Getting logical indexes
- Matrix indexing with two vectors of indices
- middleAsColumn: Return all but first and last element as a column vector
- Return elements unique to either input
- "Low : High - Low : High - Turn around " -- Create a subindices vector
- Return unique values without sorting
- Find the largest value in the 3D matrix
- Specific Element Count
- First non-zero element in each column
Problem Recent Solvers330
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!