Effacer les filtres
Effacer les filtres

Index of repeated values

3 vues (au cours des 30 derniers jours)
b
b le 22 Déc 2021
Commenté : b le 22 Déc 2021
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];

Réponse acceptée

KSSV
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)
out{1} = 1 out{2} = 5 6 7 out{3} = 12 13 14
  1 commentaire
b
b le 22 Déc 2021
Many thanks.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Data Types 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!

Translated by