Concatenate two structures from a starting point

3 views (last 30 days)
Assume I have two structures with the same fields x and y . Both of them have the fields x.dates and y.dates which are the usual dates in increasing order and x.prices with some prices for each day for a number of variables .In other words if i put 10 number of prices and d1,d2 the number of dates respectively the x.prices has size (d1,10) y.prices has size (d2,10).
I want to concatenate these structures from a starting point in the following way . Assuming the intersection of d1 and d2 is non void and k some place in the intersection :how do I make a structure of the same type which has d dates as the the d1 dates until k and d2 after and similar the prices until k are the prices from x while after they are taken from y ?
Many thanks
  2 Comments
Jan
Jan on 17 Jun 2022
A short example would be better then letting the readers guess, what "the usual dates" are.
I do not understand thuis sentence: "if i put 10 number of prices and d1,d2 the number of dates respectively the x.prices has size (d1,10) y.prices has size (d2,10)." What is "a starting point"? The meaning of the 2nd paragraph is not clear to me also.
Rangesh
Rangesh on 22 Sep 2023
Hi Mihai,
In my understanding, you want to create a new struct having first “k” elements of struct “x” and remaining from struct “y”.
To solve it, first you can find the index “k” at which the intersection of dates of the struct “x” and “y” happens. Then, create a new struct “z” with same fields and store the fields as z.dates=[x.dates(:k) y.dates] and z.prices=[x.prices(:k) y.prices].
You can refer to the following MATLAB Documentations for more information:
I hope this resolves your query.
Thanks,
Rangesh.

Sign in to comment.

Answers (1)

Jon
Jon on 22 Sep 2023
Edited: Jon on 22 Sep 2023
I think this example shows how to do what you are asking for
% Make some example data
d1 = datetime(2023,1,10):days(1):datetime(2023,8,15);
d2 = datetime(2023,7,1):days(1):datetime(2023,10,23);
x.date = d1;
x.prices = rand(numel(d1),10);
y.date = d2;
y.prices = rand(numel(d2),10)
y = struct with fields:
date: [01-Jul-2023 02-Jul-2023 03-Jul-2023 04-Jul-2023 05-Jul-2023 06-Jul-2023 07-Jul-2023 08-Jul-2023 09-Jul-2023 10-Jul-2023 … ] (1×115 datetime) prices: [115×10 double]
% Find intersection of dates
[C,ia] = intersect(x.date,y.date)
C = 1×46 datetime array
Columns 1 through 14 01-Jul-2023 02-Jul-2023 03-Jul-2023 04-Jul-2023 05-Jul-2023 06-Jul-2023 07-Jul-2023 08-Jul-2023 09-Jul-2023 10-Jul-2023 11-Jul-2023 12-Jul-2023 13-Jul-2023 14-Jul-2023 Columns 15 through 28 15-Jul-2023 16-Jul-2023 17-Jul-2023 18-Jul-2023 19-Jul-2023 20-Jul-2023 21-Jul-2023 22-Jul-2023 23-Jul-2023 24-Jul-2023 25-Jul-2023 26-Jul-2023 27-Jul-2023 28-Jul-2023 Columns 29 through 42 29-Jul-2023 30-Jul-2023 31-Jul-2023 01-Aug-2023 02-Aug-2023 03-Aug-2023 04-Aug-2023 05-Aug-2023 06-Aug-2023 07-Aug-2023 08-Aug-2023 09-Aug-2023 10-Aug-2023 11-Aug-2023 Columns 43 through 46 12-Aug-2023 13-Aug-2023 14-Aug-2023 15-Aug-2023
ia = 46×1
173 174 175 176 177 178 179 180 181 182
% Set prices before the overlap based on x, and thereafter on y
z.date = [x.date(1:ia(1)-1),y.date];
z.prices = [x.prices(1:ia(1)-1,:);y.prices]
z = struct with fields:
date: [10-Jan-2023 11-Jan-2023 12-Jan-2023 13-Jan-2023 14-Jan-2023 15-Jan-2023 16-Jan-2023 17-Jan-2023 18-Jan-2023 19-Jan-2023 … ] (1×287 datetime) prices: [287×10 double]

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!