I want to find unique value(s) based on a given conditions
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Tulkkas
le 2 Août 2022
Commenté : Jeffrey Clark
le 3 Août 2022
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 commentaires
Réponse acceptée
Jeffrey Clark
le 3 Août 2022
@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 commentaires
Jeffrey Clark
le 3 Août 2022
@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);
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Shifting and Sorting Matrices dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!