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

2 ビュー (過去 30 日間)
Angelavtc
Angelavtc 2020 年 9 月 6 日
編集済み: Adam Danz 2020 年 9 月 6 日
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

採用された回答

Adam Danz
Adam Danz 2020 年 9 月 6 日
編集済み: Adam Danz 2020 年 9 月 6 日
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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeTables についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by