Concatenate two structures from a starting point
1 view (last 30 days)
Show older comments
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
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
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.
Answers (1)
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)
% Find intersection of dates
[C,ia] = intersect(x.date,y.date)
% 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]
0 Comments
See Also
Categories
Find more on Calendar in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!