How to know the date to which a group (findgroups) corresponds?

2 views (last 30 days)
Hello!
I have a timetable (TT) where one column is a Date (dd/mm/yyyy format) and another is an Hour, I applied the following code to classify them by groups:
groups_TT = findgroups(TT.Date, TT.Hour);
This gives me a unique number for each day and hour. My question is, how can I get an identifier from this number that tells me the Date it corresponds to? The unique number is not as useful for me to perform my analysis. I need to know the date that this number corresponds to.
I attach a small example. Here my code will give me a unique identifier (from 1 to 6) and I would like to create an additional column that tells me what date each one corresponds to (if possible another table with two columns: unique id and date)
Thanks in advance!
Angela

Accepted Answer

Adam Danz
Adam Danz on 6 Sep 2020
Edited: Adam Danz on 6 Sep 2020
findgroups()
Use the 2nd output [G,ID] = findgroups(A)
G(i) belongs to ID(G(i),:). For example,
A = ["D" "B" "B" "A" "D" "D" "D"];
[G,ID] = findgroups(A)
% G =
% 3 2 2 1 3 3 3
% ID =
% 1×3 string array
% "A" "B" "D"
So, G(1) shows that A(1) is in group #3. Group #3 is ID(3) or ID(G(1)) which is "D".
To find all samples in your data that belong to a specific group,
isGroupMember = G==find(ID=="D"); % or strcmp(ID,"D")
% or, G==3
isGroupMember is a logical vector the same size as A and contains 1s (True) where A=="D".
unique()
Note that [ID,~,G] = unique(A) produces the same outputs (but G is a column vector)!
Use [ID,~,G] = unique(A,'rows') when A is a matrix and the columns contain the grouping variables.
% ID =
% 1×3 string array
% "A" "B" "D"
% G =
% 3
% 2
% 2
% 1
% 3
% 3
% 3

More Answers (0)

Categories

Find more on Tables in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!