Concatenate two structures from a starting point
조회 수: 1 (최근 30일)
이전 댓글 표시
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
Jan
2022년 6월 17일
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
2023년 9월 22일
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.
답변 (1개)
Jon
2023년 9월 22일
편집: Jon
2023년 9월 22일
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Calendar에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!