Union WITH repetitions - indices for both

1 回表示 (過去 30 日間)
Cary
Cary 2015 年 7 月 30 日
コメント済み: Walter Roberson 2015 年 7 月 30 日
How can this be achieved? I am trying to index trading days and calendar days so I can match the two together. Here is my code:
tradeDates=tradeDates(:,1); % grab trading days
calDays=datetime([2008,08,25])+caldays(0:2522)'; % grab calendar days
calDays=yyyymmdd(calDays); % convert to yyymmdd format
[C,ia,ib]=union(tradeDates,calDays); % set union of arrays
Thank you.

回答 (1 件)

Walter Roberson
Walter Roberson 2015 年 7 月 30 日
C = union(tradeDates,calDays);
[tf, idxtrade] = ismember(C, tradeDates);
[tf, idxcal] = ismember(calDays, C);
  2 件のコメント
Cary
Cary 2015 年 7 月 30 日
Thanks, I see that your code advice makes the tradeDates vector the same length as the calDays vector, but how do I generate an index for it? I am having a lot of trouble with this.
Walter Roberson
Walter Roberson 2015 年 7 月 30 日
Sorry I was inconsistent there as I was trying both ways and forgot to edit properly.
C = union(tradeDates,calDays);
[tf, idxtrade] = ismember(tradeDates, C);
[tf, idxcal] = ismember(calDays, C);
idxtrade(K) will be the index of tradeDates(K) within the union C, and idxcal(L) will be the index of calDays(L) within C.
I found it ambiguous as to what you wanted the result to be, the index of what into which?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCalendar についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by