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.
0 个评论
采纳的回答
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
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 Exchange 中查找有关 Shifting and Sorting Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!