Different groups of 1s
Afficher commentaires plus anciens
Have been trying to solve this, but couldn't
Any help would be appreciated
I have an array A=[-1 1 -1 -1 -1 -1 1 -1 1 -1 1 -1 -1 -1]
i want to get different groups of 1s like group1 contains ones with length 1 (for consecutive ones), second group contains ones with length 2 ,etc.
same approach for -1s
For 1s output shoud be [ 3 0 0 0 0 0 0 0 0 0 0 0 0 0 ]
For -1s outputs shd be [3 0 1 1 0 0 0 0 0 0 0 0 0 0 ].
1 commentaire
Matt J
le 25 Mar 2021
Why is the output [3 0 0 0 0 0 0,...]? I see 4 groups of length 1 in A.
Réponse acceptée
Plus de réponses (1)
Using the tools in this File Exchange submission,
A=[-1 1 -1 -1 -1 -1 1 -1 1 -1 1 -1 -1 -1];
map=(A==-1);
L=groupFcn(@sum, map, groupTrue(map));
output=histcounts(L,1:numel(A)+1)
Catégories
En savoir plus sur Creating and Concatenating Matrices dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!