Index of repeated values
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
For the binary vectors of the following type:
ad=[1 1 1 1 0 0 1 1 1 1]
ad=[0 0 1 1 1 1 0 0 1 1]
ad=[0 1 0 1 1 1 1 1 0 0]
ad=[1 0 0 0 1 1 1 0 0 0 0 1 1 1 0 0]
suppose we are interested in finding the index values of the ad vector with consecutive 1s (or 0s). Without using mex, how can we obtain the following output:
For the first case,
output1=[1 2 3 4]; output2=[7 8 9 10];
For the second case,
output1=[3 4 5 6]; output2=[9 10];
For the third case,
output=[4 5 6 7 8];
For the fourth case,
output1=[5 6 7]; output2=[12 13 14];
0 commentaires
Réponse acceptée
KSSV
le 22 Déc 2021
ad=[1 0 0 0 1 1 1 0 0 0 0 1 1 1 0 0] ;
ad0 = zeros(size(ad)) ;
ad0(logical(ad)) = find(ad) ;
ii = zeros(size(ad0));
jj = ad0 > 0;
ii(strfind([0,jj(:)'],[0 1])) = 1;
idx = cumsum(ii).*jj;
out = accumarray( idx(jj)',ad0(jj)',[],@(x){x'});
celldisp(out)
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Operators and Elementary Operations dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!