Group number identification consecutive numbers
Afficher commentaires plus anciens
Hello
I have millions of 1-0 data in my database. I need to detect groups of consecutive units. I would like to ask you to create the attribute "Gr_numb". An example is given in the attached file. The searched attribute is calculated there. But in Excel it is very complicated and unusable for a large database.
Thank You
6 commentaires
Image Analyst
le 11 Nov 2022
I don't understand how you're getting group numbers. In your data matrix, many, many rows are all zeros yet you somehow come up with a group number for them, and different rows of zeros have different group numbers. How are you computing group numbers for those rows?
Bruno Luong
le 11 Nov 2022
@Image Analyst don't look at the row, look only the column
- Input : (C ) Index
- Desired output (B) Gr_number
Image Analyst
le 11 Nov 2022
I still don't understand. Let's look at the upper left part of this matrix:

OK, so row 3 has a 3 in column 3, to Gr_numb is 3. But row 4 is also called group 3 in the Gr_numb column despite the fact that there in nothing in any of the columns except zeros. Why do rows 4 and 5 get assigned a group number of 3? Row 6 which was all zeros just like rows 4 and 5 is not assigned group 3. Why not?
Same for row 9. It's 3 in column 3 but a different 3 so why is it not a new group number? And why are rows 10 and 11 group 3 but not row 12???
Bruno Luong
le 11 Nov 2022
Modifié(e) : Bruno Luong
le 11 Nov 2022
It replaces the consecutive 1s with the number of 1s in this interval
Example! there are three 1s in rows 3, 4, 5, they will be replaced by three 3s since the number of 1s is 3 (three the length of group of 1s).
Image Analyst
le 11 Nov 2022
OK I think I see now, though it is probably a poor example since the second group and third groups both have a group label of 3. I don't see why two different groups should have the same group number, but the example would have been clearer if the third group had had 4 or 5 1's in it instead of 3 (same as the prior group). You must have the Mind Reading Toolbox Bruno.
@Image Analyst: The term "group number" is misleading. "Number of group members" would be clearer.
Réponse acceptée
Plus de réponses (1)
X = [1;0;1;1;1;0;0;0;1;1;1;0;1;1;0;0;1;1;0;1;0;0;1;1;0;1;1;1;1;0;1;1;1;1;1;;0;1;1;1;1;1;1;1;1;1;0;0;0;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;0]
D = diff([0;X;0]);
B = find(D>0);
E = find(D<0);
L = E-B;
G = X;
G(X==1) = repelem(L,L)
Catégories
En savoir plus sur Axis Labels dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!