Effacer les filtres
Effacer les filtres

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

3 vues (au cours des 30 derniers jours)
Tulkkas
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.

Réponse acceptée

Jeffrey Clark
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
Tulkkas
Tulkkas le 3 Août 2022
Great that does the trick, thx!
Jeffrey Clark
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);

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Calendar dans Help Center et File Exchange

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by