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

2 次查看(过去 30 天)
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 个评论
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 CenterFile Exchange 中查找有关 Shifting and Sorting Matrices 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by