I want to find unique value(s) based on a given conditions

2 ビュー (過去 30 日間)
Tulkkas
Tulkkas 2022 年 8 月 2 日
コメント済み: Jeffrey Clark 2022 年 8 月 3 日
I have 2 arrays a and b:
a = [datetime(2020,1,2), datetime(2020,1,5), datetime(2020,1,6), datetime(2020,2,3), datetime(2020,2,8),datetime(2020,3,4),datetime(2020,3,10),datetime(2020,4,11)]'
b = {'2020_01';'2020_01';'2020_01';'2020_02';'2020_02';'2020_03';'2020_03';'2020_04'}
I want to find the indices of unique element of b and if there are ties, choose the most recent based on a.
in the case, the indices that I would like to identify are the following:
indices = [0,0,1,0,1,0,1,1]'
I can of course do it with a loop but I am interested in a vectorize solution using unique, which I was not able to figure out so far.

採用された回答

Jeffrey Clark
Jeffrey Clark 2022 年 8 月 3 日
@Tulkkas, this should do it (I added a randomize of your sorted data since first, if it would always be sorted skip as indicated):
a = [datetime(2020,1,2), datetime(2020,1,5), datetime(2020,1,6), datetime(2020,2,3) ...
, datetime(2020,2,8),datetime(2020,3,4),datetime(2020,3,10),datetime(2020,4,11)]';
b = {'2020_01';'2020_01';'2020_01';'2020_02';'2020_02';'2020_03';'2020_03';'2020_04'};
% setup data assuming future datasets won't be sorted already
L = length(a);
I = randperm(L);
a = a(I);
b = b(I);
% sort datasets
[a,i] = sort(a);
b = b(i);
% extract unique data
[bu,ui] = unique(b,'last');
au = a(ui);
  2 件のコメント
Tulkkas
Tulkkas 2022 年 8 月 3 日
Great that does the trick, thx!
Jeffrey Clark
Jeffrey Clark 2022 年 8 月 3 日
@Tulkkas, or if you want the last two lines (the actual work) can be:
indices = [string(b(1:end-1))~=string(b(2:end));false];
indices(end) = indices(end-1);

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

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by